0

Here is my JSON data:

{
 "comments": [{
 "id": "1",
 "message": "Finish as soon as possible! Cibai!",
 "task_id": "1",
 "user_id": "1",
 "date_created": "2015-02-06 00:00:00.000000"
 }, {
 "id": "19",
 "message": "Another message",
 "task_id": "1",
 "user_id": "1",
 "date_created": "2015-02-10 00:00:00.000000"
 }, {
 "id": "20",
 "message": "Comment about the header",
 "task_id": "1",
 "user_id": "1",
 "date_created": "2015-02-09 00:00:00.000000"
 }],
 "status": true
}

Here is my jQuery, problem is Iam getting null in the alert:

var ids = [];
$.each(e, function(i, item) {
 ids.push(item.id);
});
alert(JSON.stringify(ids));

Thanks

Rory McCrossan
338k41 gold badges322 silver badges353 bronze badges
asked Feb 11, 2015 at 13:52

1 Answer 1

4

The id property is part of the objects stored in the comments array. You need to loop over e.comments instead. Also, as you're building an array you can use map() instead of each():

var ids = $.map(e.comments, function(item) {
 return item.id;
});
alert(JSON.stringify(ids));

Example fiddle

answered Feb 11, 2015 at 13:53
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.