14

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

[
    {
        "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:

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

1
  • 1
    does 'no' in your question mean 'number'? Commented Dec 13, 2011 at 12:35

8 Answers 8

33

You need to access the result object on iteration.

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;
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer helped my JSON array rid itself from bad data. But why is that data getting sent through? How come you have to check that extra if?
7

You could use jQuery's $.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.

1 Comment

If you want to count, you should not return false, of course.
2

Note that your object is an array of JavaScript objects. 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);

Comments

0

This will find the number of occurrences of the MNGR_NAME key in your Object Array:

var numMngrName = 0;

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

Comments

0

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

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

Comments

0

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/

Comments

0

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

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

Comments

0

Use ES6...

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

Thanks.

Comments

Your Answer

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.