Object.entries()
Temitope Ayodele
Posted on May 9, 2020
Object.entries() is used for listing all key-value pairs in an object. It accepts an object as a argument, and returns an array enumerating the key-value pairs of an object.
const obj = {
1: 'Israel',
2: 'Temi',
3: 'Miro'
};
console.log(Object.entries(obj))
//Expected result: [ ["1", "Israel"], ["2", "Temi"], ["3", "Ayo"]]
Another example:
const obj1 = {
a: 'Hello',
b: 28
};
for (let [key, value] of Object.entries(obj1)) {
console.log(`${key}: ${value}`);
}
//Expected result:
// "a: Hello"
// "b: 28"
If the argument passed is not an object, it causes TypeError,
If the key passed in the argument is not in the range of the property[key, value] pair, it causes RangeError.
š šŖ š
š©
Temitope Ayodele
Posted on May 9, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.