【问题标题】:loop a JS Object循环一个 JS 对象
【发布时间】:2012-01-15 07:33:54
【问题描述】:

我有这个 JS 对象:

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

如何遍历这些项目以获取评论值(“comment”:“text”)?

【问题讨论】:

  • 为什么要循环播放?你可以直接使用object.data.nid[0].comment

标签: javascript object loops


【解决方案1】:

你真的不需要循环来获取它。就这样吧……

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

var text = obj.data.nid[0].comment;

或者如果有多个,可以使用forEach...

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

或者传统的for循环...

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

或者如果你想构建一个数组,使用map...

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

或者又是一个传统的for 循环......

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

【讨论】:

  • 谢谢,但如果 nid 更改为 number ? {“数据”:{“1222”:[{“cid”:“32”,“uid”:“780”,“评论”:“文本”}]},“请求状态”:“找到”}
  • @Laky:然后将obj.data.nid 更改为obj.data[1222]。或者你是说它可能是一个不同的数字,或者是一组数字?
【解决方案2】:

给定:

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

获取评论的直接方式是:

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

至于“循环”通过项目来获取价值,我不确定循环的意义。你是说你可能不知道对象的结构,但你知道它会在某个地方有一个“评论”字段?

“nid”数组中只有一个项目 - 如果这只是一个示例,但实际上您将拥有一个包含更多值的数组,您可以循环遍历该数组:

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"]);
}

【讨论】:

    【解决方案3】:

    如果您只是引用该特定对象(或者如果您使用的每个对象都遵循相同的模式),那么您可以直接访问该值:

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

    如果你想做一些迭代的事情,那么不妨试试这个:

    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);
        }
    }
    

    或者如果你真的想迭代地做整个的事情:

    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);
    

    【讨论】:

      猜你喜欢
      • 2014-07-28
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      • 1970-01-01
      • 1970-01-01
      • 2021-07-25
      • 2016-03-17
      • 2015-07-03
      相关资源
      最近更新 更多