user3213490 user3213490 - 1 year ago 93
jQuery Question

How to get JSON object values for a Particular key

I am using JQGRId to get the grid data in JSON object format.

Result is something like :

{"Key":"value1"},{"Key":"value2", "id":"id"}

I need to get all the values into an array for "key".

Answer Source

var arr = [{"Key":"value1"},{"Key":"value2", "id":"id"}];
var anArrayOfKeyValues = [];

arr.forEach(function(ob, i) {
  if("Key" in ob) anArrayOfKeyValues.push(ob.Key);

console.log(anArrayOfKeyValues); // ["value1", "value2"]

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download