JQuery Array Keep Returning [Object Object]

JQuery Array Keep Returning [Object Object]

I am creating a jQuery array but my code keeps returning [object Object], 3 times.

Here is my code:

var idArray = [];
$('.taskPHP').each(function() {
  var idStr = { 
    'taskPHPid': $(this).attr('id'), 
    'taskValue': $(this).val() 
  }
  idArray.push(idStr);
});

alert(idArray);

EDIT: Just call me stupid...i knew i couldn't alert an array. err...to many beers and programming don't mix.

I will give u credit as soon as I can. Thanks for the help.

4

3 Answers

Seems fine, try console.log and see what it outputs, or alert(JSON.stringify(idArray))

0

The default "to string" for an Object is "[object Object]".

This is what the Array.toString implementation is calling for all of the elements it contains. (Array has a standard "pretty" string representation, but only in a shallow fashion.)

Stringify it to JSON first (i.e. JSON.stringify) or use a smart console.log (i.e. like Chrome's) that doesn't just call [[ToString]] on the result ..

You can't view Objects in an alert() window.

If you want to see the contents of the Object, use:

console.log(idArray); // doesn't work in IE of course

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Elena Rostova
Author

Elena Rostova

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.