Javascript 如何循环遍历 JSON 数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8489288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:12:30  来源:igfitidea点击:

How to loop through JSON array?

javascriptjsonjquery

提问by pal

I have some JSON-code which has multiple objects in it:

我有一些 JSON 代码,其中包含多个对象:

[
    {
        "MNGR_NAME": "Mark",
        "MGR_ID": "M44",
        "EMP_ID": "1849"
    },
    {
        "MNGR_NAME": "Steve",
        "PROJ_ID": "88421",
        "PROJ_NAME": "ABC",
        "PROJ_ALLOC_NO": "49"
    }
]

My JSON loop snippet is:

我的 JSON 循环片段是:

function ServiceSucceeded(result) 
{       
  for(var x=0; x<result.length; x++) 
  {      

  }    
}

Could you please let me know how to check there is no occurence of "MNGR_NAME" in the array. (It appears twice in my case.)

您能否让我知道如何检查数组中没有出现“MNGR_NAME”。(在我的情况下它出现了两次。)

回答by Abdul Munim

You need to access the resultobject on iteration.

您需要result在迭代时访问该对象。

for (var key in result)
{
   if (result.hasOwnProperty(key))
   {
      // here you have access to
      var MNGR_NAME = result[key].MNGR_NAME;
      var MGR_ID = result[key].MGR_ID;
   }
}

回答by Kees C. Bakker

You could use jQuery's $.each:

你可以使用 jQuery 的$.each

    var exists = false;

    $.each(arr, function(index, obj){
       if(typeof(obj.MNGR_NAME) !== 'undefined'){
          exists = true;
          return false;
       }
    });

    alert('Does a manager exists? ' + exists);

Returning falsewill break the each, so when one manager is encountered, the iteration will stop.

返回false将破坏 each,因此当遇到一个 manager 时,迭代将停止。

回答by Thomas Johan Eggum

Note that your object is an array of JavaScript objects. Could you use something like this?

请注意,您的对象是一个 JavaScript 对象数组。你能用这样的东西吗?

var array = [{
    "MNGR_NAME": "Mark",
    "MGR_ID": "M44",
    "EMP_ID": "1849"
},
{
    "MNGR_NAME": "Steve",
    "PROJ_ID": "88421",
    "PROJ_NAME": "ABC",
    "PROJ_ALLOC_NO": "49"
}];

var numberOfMngrName = 0;
for(var i=0;i<array.length;i++){
    if(array[i].MNGR_NAME != null){
        numberOfMngrName++;
    }
}

console.log(numberOfMngrName);

回答by user1349544

Use ES6...

使用 ES6...

myobj1.map(items =>
{
if(items.MNGR_NAME) {
return items.MNGR_NAME;
}else {
//do want you want.
}    
})

Thanks.

谢谢。

回答by jabclab

This will find the number of occurrences of the MNGR_NAMEkey in your ObjectArray:

这将MNGR_NAME在您的 中找到键的出现次数ObjectArray

var numMngrName = 0;

$.each(json, function () {
    // 'this' is the Object you are iterating over
    if (this.MNGR_NAME !== undefined) {
        numMngrName++;
    }
});

回答by Alex K.

Within the loop result[x]is the object, so if you wanted to count a member that may or may not be present;

循环内result[x]是对象,所以如果你想计算一个可能存在也可能不存在的成员;

function ServiceSucceeded(result)
{
  var managers = 0
  for(var x=0; x<result.length; x++)
  {
        if (typeof result[x].MNGR_NAME !== "undefined")
            managers++;
  }
  alert(managers);
}

回答by kgiannakakis

You could use $.each or $.grep, if you also want to get the elements that contain the attribute.

如果您还想获取包含该属性的元素,则可以使用 $.each 或 $.grep。

filtered = $.grep(result, function(value) {
    return (value["MNGR_NAME"] !== undefined)
});
count = filtered.length

回答by Steffen Stenersen

You can iterate over the collection and check each object if it contains the property:

您可以遍历集合并检查每个对象是否包含该属性:

var count = 0;
var i;
for(i = 0; i < jsonObj.length; i += 1) {
    if(jsonObj[i]["MNGR_NAME"]) {
        count++;
    }
}

Working example: http://jsfiddle.net/j3fbQ/

工作示例:http: //jsfiddle.net/j3fbQ/