美文网首页
JS entries

JS entries

作者: 我的天气很好啦 | 来源:发表于2019-10-16 17:17 被阅读0次

    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

          本文链接:https://www.haomeiwen.com/subject/nudumctx.html