Get the key and value of the object

1 Object.keys()

ES5 introduces the Object.keys method, which returns an array. The members are the key names of all the enumerable properties of the parameter object itself (excluding inheritance).

var obj = { key1: "val1", key2: val2};Object.keys(obj)// ["key1", "key2"]

At present, ES7 has a proposal to introduce Object.values and Object.entries matching Object.keys.

  let {keys, values, entries} = Object;
  let obj = {a: 1, b: 2, c: 3};
  for (let key of keys(obj)) {
  console.log(key);
   // 'a', 'b', 'c'}for (let value of values(obj)) {	console.log(value); // 1, 2, 3}for (let [key, value] of entries(obj)) {console.log([key, value]); // ['a', 1], ['b', 2], ['c', 3]}

2 Object.values()
The Object.values method returns an array whose members are the key values of all enumerable properties of the parameter object itself (excluding inheritance).

  var obj = { key1: "val1", key2: val2 };Object.values(obj)// ["val1", val2]

The member order of the returned array is consistent with the arrangement rules described in the traversal of attributes in this chapter.

    var obj = { 100: 'a', 2: 'b', 7: 'c' };Object.values(obj)// ["b", "c", "a"]

In the above code, the attribute named value is traversed from small to large according to the value, so the return order is b, c and a.
Object.values returns only the traversable properties of the object itself.

    var obj = Object.create({}, {p: {value: 42}});Object.values(obj) // []

In the above code, the object attribute (attribute p) added to the second parameter of the Object.create method is not traversable by default if it is not explicitly declared. Object.values does not return this property.

    Object.values The filter property name is Symbol Property of the value. Object.values({ [Symbol()]: 123, foo: 'abc' });// ['abc']

If the parameter of the Object.values method is a string, an array of characters will be returned.

Object.values('foo')// ['f', 'o', 'o']

In the above code, the string will first be converted into an array like object. Each character of the string is an attribute of the object. Therefore, Object.values returns the key value of each attribute, which is an array of characters.
If the parameter is not an object, Object.values first converts it to an object. Because of the wrapper objects of numeric and Boolean values, non inherited properties are not added to the instance. Therefore, object. Values returns an empty array.

    Object.values(42) // []Object.values(true) // []

3 Object.entries
The Object.entries method returns an array whose members are an array of key value pairs of all the enumerable properties of the parameter object itself (excluding inheritance).

    var obj = { foo: 'bar', baz: 42 };Object.entries(obj)// [ ["foo", "bar"], ["baz", 42] ]

Except that the return value is different, the behavior of this method is basically the same as that of Object.values.
If the property name of the original object is a Symbol value, the property will be omitted.

    Object.entries({ [Symbol()]: 123, foo: 'abc' });// [ [ 'foo', 'abc' ] ]

In the above code, the original object has two attributes. Object.entries only outputs attributes with attribute names other than Symbol values. In the future, there may be a Reflect.ownEntries() method that returns all the properties of the object itself.
The basic purpose of Object.entries is to traverse the properties of an object.

    var obj = { foo: 'bar', baz: 42 };
    var map = new Map(Object.entries(obj));
    map // Map { foo: "bar", baz: 42 }

Implement the Object.entries method yourself, which is very simple

 function* entries(obj) {
            for (let key of Object.keys(obj)) {
                yield [key, obj[key]];
            }
        }
        //  Version of non Generator function
        // function entries(obj) {	
            // let arr = [];	
            // for (let key of Object.keys(obj)) {		
            // arr.push([key, obj[key]]);	
        // }	
        //  return arr;
        // }

Original text from: https://blog.csdn.net/yexudengzhidao/article/details/89212348

Posted by matt_wilkes on Thu, 02 Dec 2021 20:52:19 -0800