JS entries

Object.entries() return an array whose elements are arrays corresponding to the enumerable string-keyed property[key, value]pairs found directly upon object.The ordering of the properties is the same as that given by looping over the property values of the object manually.
Object.entries()的执行结果是返回一个二维数组,其元素是与直接在object上找到的可枚举key-value相对应的数组(将他们一起放在一个数组里),属性的顺序与通过手动循环对象的属性值所给出的顺序相同。

如果Object里有key的值是number类型,则类型为number的key,会默认按从小到大顺序展示。

例如:

const obj = {foo: 'bar', baz: 42}
console.log(Object.entries(obj))
// [ ['foo', 'bar'], ['baz', 42] ]

const obj = {0: 'a', 1: 'b', 2: 'c'}
console.loh(Object.entries(obj))
// [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ]

const obj = {100: 'a', 2: 'b', 7: 'c'}
console.loh(Object.entries(obj))
// [ ['2', 'b'], ['7', 'c'], ['100', 'a'] ]

如果需要把通过entries方法得到的数组再变成对象,则使用Map

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

你可能感兴趣的:(JS entries)