In this article, we will explore the methods for retrieving key values from a JavaScript Object.
We will learn how to retrieve a key, obtain a value, and extract a key-value pair from a JavaScript Object.
Getting a key from a JavaScript Object
There are various methods used to get a key from JavaScript Object which are as follows:
- Utilizing the for-in loop
- Utilizing the find Method
- Utilizing filter Method and Object keys Method
- Utilizing Object.entries and reduce Method
- Utilizing Lodash _.findKey Method
We will thoroughly examine each method individually, one at a time.
Utilizing the for-in loop
The for-in loop can be employed to retrieve a key from a JavaScript Object using the following code.
Code:
function getKeyByValue(object, value) {
for (let prop in object) {
if (object.hasOwnProperty(prop)) {
if (object[prop] === value)
return prop;
}
}
}
const exampleObject = {
key1: 'JTP',
key2: 50,
key3: 'JavaScript'
};
outcome = getKeyByValue(exampleObject, 50);
console.log(outcome);
Output:
It is evident that we have obtained a key with a value of 50 through the use of the for-in loop.
Utilizing a find Method
The Object.keys method is employed to retrieve all keys from a JavaScript object. Additionally, we can use the find method to locate a specific key by utilizing the following code.
Code:
function getKeyByValue(object, val) {
return Object.keys(object).find(key =>
object[key] === val);
}
const exampleObject = {
key_1: 'JTP',
key_2: 250,
key_3: 'JavaScript'
};
result = getKeyByValue(exampleObject, 'JavaScript');
console.log(result);
Output:
In the output, we observe that we have retrieved a key associated with the value JavaScript by employing the find Method.
Utilizing filter Method and Object keys Method
By employing the filter method in conjunction with the Object.keys method, we can extract a key using the provided code.
Code:
function getKeyByValue(obj, val) {
return Object.keys(obj)
.filter(key => obj[key] === val);
}
const exampleObject = {
key1: 'JTP',
key2: 100,
key3: 'JavaScript'
};
outcome = getKeyByValue(exampleObject, 'JTP');
console.log(outcome);
Output:
In the output presented, we observe that by employing the filter method in conjunction with the Object.keys method, we have successfully retrieved a key associated with the value JTP.
Utilizing Object.entries and reduce Method
The code provided demonstrates the use of the Object.entries function in conjunction with the reduce method to extract a key.
Code:
function getKeyByValue(ob, val) {
return Object.entries(ob)
.reduce((z, [key, val1]) => {
if (val1 === val) {
z.push(key);
}
return z;
}, []);
}
const exampleObject = {
key1: 'JTP',
key2: 'Tutorial',
key3: 86
};
res = getKeyByValue(exampleObject, 86);
console.log(res);
Output:
The output displayed demonstrates that the key corresponding to the value of 86 has been identified through the use of the Object.entries method in conjunction with the reduce method.
Utilizing Lodash _.findKey Method
We will employ the _.findKey function to retrieve a key by using the following code.
Code:
const _ = require("lodash");
let products = {
'Air Conditioner': { 'Price': 45000 },
'Microwave Oven': { 'Price': 39000 },
'Refrigerator': { 'Price': 25000 }
};
let found_key = _.findKey(products, {
'Price': 45000
});
console.log(found_key);
Output:
In the output displayed, we can observe that a key associated with the value of 45000 has been identified through the use of the _.findKey function.
Getting a value from a JavaScript Object
The Object.values method is a function that allows us to extract values from a JavaScript Object.
Object.values method
The Object.values method can be employed to retrieve a value.
Demo-1:
We will retrieve the values from the object named "students" by employing the Object.values method.
Code:
const students = {
name : 'Anurag',
age : 15,
standard : '10th',
address : 'Noida',
};
console.log(Object.values(students));
Output:
We can distinctly observe all the values contained within the JavaScript Object referred to as "students," presented in the format of an array.
Demo-2:
We will retrieve the values from the object named "products" by employing the Object.values method.
Code:
const products = {
company : 'Dettol',
name : 'Sanitizer',
quantity : 150,
price : 18000
};
console.log(Object.values(products));
Output:
We can observe all the values contained within the JavaScript Object referred to as "products" structured as an array.
Getting a key-value pair from the JavaScript Object
A method known as Object.entries is available for retrieving key-value pairs from a JavaScript Object.
Object.entries method
The Object.entries function can be employed to retrieve a pair consisting of keys and values.
Demo-1:
We will retrieve the key-value pair associated with "employees" by employing the Object.entries method.
Code:
let employees ={
name : "Prerna",
age : 25,
department : 'Software Engineering',
salary : 55000,
address : 'Mumbai',
};
let entries = Object.entries(employees)
let outcome = entries.map( ([key, val] = entry) => {
return `${key} : ${val}`;
});
console.log(outcome);
Output:
We can distinctly observe the key-value pairs of the JavaScript object referred to as "employees" structured as an array.
Demo-2:
We will retrieve the key-value pairs of dayColours by employing the Object.entries function.
Code:
let dayColours = {
Monday : "White",
Tuesday : "Red",
Wednesday : "Green",
Thursday : "Yellow",
Friday : "Pink",
Saturday : "Purple and Black",
Sunday : "Orange",
};
let entries = Object.entries(dayColours)
let outcome = entries.map( ([key, val] = entry) => {
return `${key} : ${val}`;
});
console.log(outcome);
Output:
We can distinctly observe the key-value pairs of the JavaScript object referred to as "dayColours" presented in the format of an array.
Conclusion:
We have understood how to get key value from JavaScript Object in this article. Following are some points to remember:
- We can get a key from the JavaScript object utilizing the following methods: for-in loop find Method filter Method and Object keys Method entries and reduce Method Lodash _.findKey Method
- We can get a value from the JavaScript object utilizing the following method: for-in loop
- We can get a key-value pair from the JavaScript object utilizing the following method: entries
- for-in loop
- find Method
- filter Method and Object keys Method
- entries and reduce Method
- Lodash _.findKey Method
- for-in loop
- entries