3

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
  • You're missing a semi-colon from after the closing-brace }. Commented Sep 15, 2012 at 23:20
  • @DavidThomas The missing semicolon shouldn't cause a problem. Commented Sep 15, 2012 at 23:22
  • @William, that's why I left it as a comment (sloppy syntax, even while still 'correct,' bothers me is all). =) Commented Sep 15, 2012 at 23:24
  • javascript arrays are objects too. which means alerting them should yield "object". functions are objects too and they yield "object" too when alerted except internal functions which yield "function. Commented Sep 16, 2012 at 8:22

3 Answers 3

10

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

Sign up to request clarification or add additional context in comments.

Comments

1

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 ..

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.