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 }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容