how to get key and value from json array object in javascript ?

how to get key and value from json array object in javascript – JavaScript Object.keys() will return an array on object keys and you can use also it to retrieve from the object.

how to get key and value from json array object in javascript

key value json javascript

var productArr = '{"required":1, "minlength":2}'
var resultProductData = JSON.parse(productArr);
for (key in resultProductData) {
  if (resultProductData.hasOwnProperty(key)) {
      console.log("%c "+key + " = " + resultProductData[key],"color:red");
  }
}

JavaScript Object.keys()

const productObj1 = {
  a: 'laptop dell 4580',
  b: 42,
  c: false
};

console.log(Object.keys(productObj1));
// expected Results: Array ["a", "b", "c"]

example

const productObj1 = {
  a: 'laptop dell 4580',
  b: 42,
  c: false
};

for(let key of Object.keys(productObj1)){
  console.log(key);
}
// expected Results: 
// > "a"
// > "b"
// > "c"

JavaScript Object.values()

const productObj1 = {
  a: 'laptop dell 4580',
  b: 42,
  c: false
};

console.log(Object.values(productObj1));
// expected Results: Array ["laptop dell 4580", 42, false]

get values in the array

const productObj1 = {
  a: 'laptop dell 4580',
  b: 42,
  c: false
};

for(let value of Object.values(productObj1)){
  console.log(value);
}
// expected Results: 
// > "laptop dell 4580"
// > 42
// > false

Don’t Miss : How To Get Key And Value From Json Array Object In Javascript?

Read Also:  jquery set data attribute

JavaScript Object.entries()
JavaScript Object.entries() method

const productObj1 = {
  a: 'laptop dell 4580',
  b: 42
};

console.log(Object.entries(productObj1));

// expected Results:
// Array [Array ["a", "laptop dell 4580"], Array ["b", 42]]

use a key value loop to get key and value at a time.

const productObj1 = {
  a: 'laptop dell 4580',
  b: 42
};
for (const [key, value] of Object.entries(productObj1)) {
  
  console.log(`${key}: ${value}`);
}

// expected Results:
// "a: laptop dell 4580"
// "b: 42"

How to Get an Object’s Keys and Values in JavaScript?

const product_object = { name: 'Daniel', price: 54545, vender: 'fnf', star: 4 };

//Getting an object’s keys
console.log(Object.keys(product_object));
// Expected Results: ["name", "price", "vender", "star"]

//Getting an object’s values
console.log(Object.values(product_object));
// Expected Results: ["Daniel", 54545, "fnf", 4]

//Getting an object’s entries
console.log(Object.entries(product_object));
// Expected Results: [["name", "Daniel"], ["price", 56546], ["vender", "fnf"], ["star", 4]]

//Reform an object
const productobjEntries = Object.entries(product_object);
console.log(Object.fromEntries(productobjEntries));
// Expected Results: {name: "DVDS", price: 152, vender: "fnf", star: 4}

//Example
const resDtl = [[4, 28, 167, "JavaScript"], ["One", "Two", "Three"], ["welcome"]];
console.log(Object.fromEntries(resDtl));
// Expected Results: {4: 28, One: "Two", welcome: undefined}

Get value for key from nested JSON object in JavaScript

we have a nested JSON object

const product_object = {
   "prop": [
      {
         "key": "Mobile",
         "value": "Mobile is wonderfull, Mobile is great"
      },
      {
         "key": "Laptop",
         "value": "Laptop is great, really great"
      }
   ]
};

Example

const product_object = {
   "prop": [
      {
         "key": "Mobile",
         "value": "Mobile is wonderfull, Mobile is great"
      },
      {
         "key": "Laptop",
         "value": "Laptop is great, really great"
      }
   ]
};
const findByKey = (product_object, key) => {
   const arr = product_object['prop'];
   if(arr.length){
      const result = arr.filter(el => {
         return el['key'] === key;
      });
      if(result && result.length){
         return result[0].value;
      }
      else{
         return '';
      }
   }
}
console.log(findByKey(product_object, 'Laptop'));

Results

Laptop is great, really great

I hope you get an idea about how to get key and value from json array object in javascript.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.