1

I have this JS object:

{ "data": { "nid": [{ "cid": "32", "uid": "780", "comment": "text" }] }, "request_status": "found" } 

how can I loop through these items to get comment value ("comment":"text")?

1
  • 1
    why do you want to loop it? You can get it directly with something like object.data.nid[0].comment Commented Jan 15, 2012 at 7:38

3 Answers 3

3

You don't really need to loop to get it. Just do...

var obj = {"data":{"nid":[{"cid":"32","uid":"780","comment":"text"}]},"request_status":"found"}; var text = obj.data.nid[0].comment; 

Or if there are several, you can use forEach...

obj.data.nid.forEach(function(val,i) { alert( val.comment ); }); 

Or a traditional for loop...

for( var i = 0; i < obj.data.nid.length; i++ ) { alert( obj.data.nid[i].comment ); } 

Or if you want to build an Array, use map...

var arr = obj.data.nid.map(function(val,i) { return val.comment; }); 

Or again a traditional for loop...

var arr = [] for( var i = 0; i < obj.data.nid.length; i++ ) { arr.push( obj.data.nid[i].comment ); } 
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, but if nid change to number ? { "data": { "1222": [{ "cid": "32", "uid": "780", "comment": "text" }] }, "request_status": "found" }
@Laky: then change obj.data.nid to obj.data[1222]. Or are you saying it could be a different number, or a set of several numbers?
1

Given:

var obj = { "data": { "nid": [{ "cid": "32", "uid": "780", "comment": "text" }] }, "request_status": "found" }; 

The direct way to retrieve the comment is:

obj["data"]["nid"][0]["comment"] // or obj.data.nid[0].comment 

As far as "looping" through the items to get the value, I'm not sure how a loop makes sense. Are you saying you might not know the structure of the object but you know it will have a "comment" field in there somewhere?

The "nid" array only has one item in it - if this was just a sample but really you'll have an array with more values you can loop through that array:

var nid = obj["data"]["nid"], // get a direct reference to the array to save i; // repeating obj.data.nid everywhere for (i=0; i < nid.length; i++) { // do something with the comment in the current item console.log(nid[i]["comment"]); } 

Comments

1

If you're just referring to that specific object (or if every object you are working with follows that same pattern), then you can just access the value directly:

var theObj = {"data":{"nid":[{"cid":"32","uid":"780","comment":"text"}]},"request_status":"found"}; alert(theObj.data.nid[0].comment); 

If you want to do something iterative, then perhaps try this:

var theObj = {"data":{"nid":[{"cid":"32","uid":"780","comment":"text"}]},"request_status":"found"}; for (var index = 0; index < theObj.data.nid.length; index++) { var item = theObj.data.nid[index]; if (item.comment) { alert(item.comment); } } 

Or if you really want to do the entire thing iteratively:

window.searchObj = function(theObj) { if (theObj.comment) { alert(theObj.comment); } if (theObj instanceof Array) { searchArray (theObj); } else if (theObj instanceof Object) { for (var key in theObj) { searchObj(theObj[key]); } } }; window.searchArray = function(theArray) { for (var index = 0; index < theArray.length; index++) { var item = theArray[index]; searchObj(item); } }; var theObj = {"data":{"nid":[{"cid":"32","uid":"780","comment":"text"}]},"request_status":"found"}; searchObj(theObj); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.