简体   繁体   English

如何循环JSON数组?

[英]How to loop through JSON array?

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. 你能否告诉我如何检查阵列中是否没有“MNGR_NAME”。 (It appears twice in my case.) (在我的案例中,它出现了两次。)

You need to access the result object 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;
   }
}

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 false will break the each, so when one manager is encountered, the iteration will stop. 返回false会破坏每个,因此当遇到一个管理器时,迭代将停止。

Note that your object is an array of JavaScript objects. 请注意,您的对象是一个JavaScript对象数组。 Could you use something like this? 你能用这样的东西吗?

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

Use ES6... 使用ES6 ......

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

Thanks. 谢谢。

This will find the number of occurrences of the MNGR_NAME key in your Object Array : 这将在Object Array找到MNGR_NAME键的出现次数:

var numMngrName = 0;

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

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

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

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/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM