0

I want to check if "T1000" exists as an approval "key" in the following JSON. If it exists, then I want to display the corresponding approval "value".

{"approvals": 
    [
         {"approval":
            {
               "id":"0121920",
               "key":"T100",
               "value":"Ben Tsu"
            }
         },
         {"approval":
            {
               "id":"",
               "key":"T1000",
               "value":"Amy Dong"
            }
         }
    ]
}

I'm getting JSON as an Ajax response. I'm trying to loop through all properties and match it to the value passed in as the parameter.

Here's my code but it only spits out object Object, object Object. So, if I have 5 properties, this code spits out object Object 5 times.

I'm passing inputFieldDefaultValue as a parameter to the plugin with the value being T1000. Hence, o.inputFieldDefaultValue.

$.each(response.approvals, function(index, approvals){ 
    if(approvals.approval.key == o.inputFieldDefaultValue){ 
         approvals.approval.value; 
    } 
}); 

If I do

$.each(response.approvals, function(index, approvals){ 
    if(approvals.approval.key == o.inputFieldDefaultValue){ 
         alert(approvals.approval.value); 
    } 
});

it alerts the corresponding value (Amy Dong) but it still writes object Object (as many times as the properties in the JSON response).

4 Answers 4

3

You may access the values of the JSON object by using the following code that traverses the objects:

var approvals = obj.approvals;

for(var i = 0; i < approvals.length; i++)
{
  if(approvals[i].approval.key = 'T1000')
  {
    // Display approvals[i].approval.value
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Here's my code and it only spits out object Object, object Object. I'm passing inputFieldDefaultValue as a parameter to the plugin and extending it. Hence, o.inputFieldDefaultValue. $.each(response.approvals, function(index, approvals){ if(approvals.approval.key == o.inputFieldDefaultValue){ approvals.approval.value; } }) If I do alert(approvals.approval.value;), then I get the corresponding value.
First, please post code as an update to your question. Second, that code is nothing like mine, so I'm not sure why you're posting it here. Third, you're not doing anything with the value in the actual code.
0

It can be done without jQuery. You just need Array.prototype.filter and Array.prototype.map:

var aListOfAllValuesFromKeysEqualsToT1000 = json.approvals.filter(function(approval) {
    return approval.key === "T1000";
}).map(function(approval) {
    return approval.value;
});

Then use aListOfAllValuesFromKeysEqualsToT1000 to do whatever you want the values for.

2 Comments

You should mention that this is only supported by recent browsers. For instance, IE9 is the first IE to support it.
True that. But now you've mentioned it for me :) +1
0

you would first take your json and use JSON.parse(json) to get some object literals. From there you would do

var ary = obj.approvals;
for (var i = 0; i < ary.length; i++) {
  var current = ary[i], val;
  if (current.key === 'T1000') {
    val = current.value;
    break;
  }
}

Comments

0

With jQuery:

var approvals = obj.approvals;
$.each(approvals, function () {
    if (this.approval.key === 'T1000') {
        alert(this.approval.value);
    }
});

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.