user3213490 user3213490 - 1 month ago 5x
jQuery Question

Ho 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".


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"]