In this post, we are going to learn how to get json object key value in JavaScript with examples.The object.entries(), object.keys(), object.values().We will get keys and values of objects and loop through key-value using for loop.
1. object.keys() to get object keys
The JavaScript object.keys() method returns an array of the given object’s own property name and iterates through them as a normal loop. It takes an object which keys names we want to get and return an array of string that contain keys of given object. We are looping through all keys of an object using a for-of loop.
let Employee ={Empname: 'John', salary: 60000,EmpID:1,Dep:'Admin'}
console.log('keys of given objects:\n',Object.keys(Employee));
console.log('\nIterate through keys:')
for(let key of Object.keys(Employee))
{
console.log(key);
}
Output
keys of given objects:
[ 'Empname', 'salary', 'EmpID', 'Dep' ]
Iterate through keys:
Empname
salary
EmpID
Dep
- How to get json object key value in JavaScript
- Check object has property in JavaScript
- Add multiple properties to array of object in JavaScript
- Delete property from objects Array in Javascript(6 ways)
- 7 ways to Remove object from array in Javascript
2. Object.values() to get object values
The JavaScript Object.values() method return an array of given object own enumerable properties values. It takes an object which enumerate values we want to get and return an array that contain values of given object. We are iterating overvalues of objects using for-of loop.
let Employee ={Empname: 'John', salary: 60000,EmpID:1,Dep:'Admin'}
console.log('values of given objects:\n',Object.values(Employee));
console.log('\nIterate through values:')
for(let value of Object.values(Employee))
{
console.log(value);
}
Output
values of given objects:
[ 'John', 60000, 1, 'Admin' ]
Iterate through values:
John
60000
1
Admin
3. object.entries() to get object keys and values
The object.entries() method return separate arrays of keys and values. We are using for-of loop to iterate over the key and value pair of the given object using for loop.
let Employee ={Empname: 'John', salary: 60000,EmpID:1,Dep:'Admin'}
for (const [key, value] of Object.entries(Employee)) {
console.log(`${key}: ${value}`);
}
Output
Empname: John
salary: 60000
EmpID: 1
Dep: Admin
4. Get nested JSON object key values in JavaScript
In this example we are accesing nested json object property by using dot and square operator.Let us understand with below example.
let Employee = {Empname: 'Jack', salary: 60000,EmpID:1,Dep:'Admin',
"address": {
"streetAddress": "United State",
"city": "New York"
},
"ContactNumber": [
{
"type": "Home",
"number": "033 112-1311"
},
{
"type": "fax",
"number": "222 222-2222"
},
{
"Email":"Abc@devenum.com"
}
]
}
console.log(Employee['Empname']);
console.log(Employee.Empname);
console.log(Employee.address.streetAddress);
console.log(Employee["address"].city);
console.log(Employee.ContactNumber[0].number);
console.log(Employee.ContactNumber[1].type);
console.log(Employee.ContactNumber[2].Email);
console.log(Employee.ContactNumber.number);
Summary
In this post, we have learned how to get JSON object key values in JavaScript with examples.The object.entries(), object.keys(), object.values().We will get keys and values of objects and loop through key-value using for-of loop.