This post talks about the object.values() method
and its examples.
object value method
As we already have an inbuilt in Object.keys()
method to iterate the keys of an object.
Before ES8, There is no inbuilt method to have supported the iteration of values.
Es8 introduced the values() method to iterate the values.
How do you iterate objects? So for-in loop is used to iterate each element of an object.
Object.values() method
returns array of values in the same order for-in loop returns the data.
Object. values return the own properties values of an object, not the values via prototype inheritance.
Syntax:
Object.values(myobject)
Parameters and returns
- Input parameters - myobject is input provided to iterate the element values
- Return type - returns an array of object values.
Let’s see an example to print properties in an array.
const obj = {
id: 's1',
name:'tom',
sal: 1000
};
console.log(Object.values(obj));
Output is
[ 's1', 'tom', 1000 ]