Object.entries() of the Object constructor method

https://blog.csdn.net/z591102/article/details/106404738/

Object.entries() of the Object constructor method

Object.entries( obj)
returns an array of key-value pairs of the enumerable properties of a given object itself, and its arrangement is consistent with the return order when using the for...in loop to traverse the object (the difference is that the for...in loop also enumerates the prototype chain Attributes in)

Generally speaking, Object.entries() can traverse the key value of an object in the form of an array, the order is the same as for...in, but it will not traverse the prototype property

description

Object.entries() returns an array whose elements are arrays corresponding to the enumerable property key-value pairs found directly on the object. The order of the attributes is the same as the order given by manually cycling through the attribute values ​​of the object.

JavaScript Demo: Object.entries()

const obj = {
  A:'hy',
  B:12
};
console.log(Object.entries(obj)) // [[‘A’,'hy'],[‘B',12]]
 
for(let [key, value] of Object.entries(obj)){
     console.log(`${key}:${value}`) // A:hy  B:12
}
将Object转换为Map

const obj = {
  A:'hy',
  B:12
};
const map = new Map(Object.entries(obj))
console.log(map) // Map { A:’hy’ , B:12 }

 

Guess you like

Origin blog.csdn.net/wwf1225/article/details/114890590