ES8中Object方法-使用说明

Object

Object.entries

返回一个给定对象自身可枚举属性的键值对数组

const kv=Object.entries(obj1);
console.log(kv); //[["name","luochao"],["age",22]]

const obj=Object.entries('lc');
console.log(obj)//[["0","l"],["1","c"]]
const arr=Object.entries([1,2]);
console.dir(arr);//[["0","1"],["1","2"]]

const s1={
  3 : 'lc',
  1 : 'yx',
  "luochao": "22"
}
console.log(Object.entries(s1)) //[["1","yx"],["3","lc"],["luochao","22"]]

const s1={
  3 : 'lc',
  1 : 'yx',
  "luochao": "22"
}
console.log(Object.entries(s1)) //[["1","yx"],["3","lc"],["luochao","22"]]

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

// array like object
const obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log(Object.entries(obj)); // [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ]

// array like object with random key ordering
const anObj = { 100: 'a', 2: 'b', 7: 'c' };
console.log(Object.entries(anObj)); // [ ['2', 'b'], ['7', 'c'], ['100', 'a'] ]

// getFoo is property which isn't enumerable
const myObj = Object.create({}, { getFoo: { value() { return this.foo; } } });
myObj.foo = 'bar';
console.log(Object.entries(myObj)); // [ ['foo', 'bar'] ]

// non-object argument will be coerced to an object
console.log(Object.entries('foo')); // [ ['0', 'f'], ['1', 'o'], ['2', 'o'] ]

// iterate through key-value gracefully
const obj = { a: 5, b: 7, c: 9 };
for (const [key, value] of Object.entries(obj)) {
  console.log(`${key} ${value}`); // "a 5", "b 7", "c 9"
}

// Or, using array extras
Object.entries(obj).forEach(([key, value]) => {
console.log(`${key} ${value}`); // "a 5", "b 7", "c 9"
});

相关推荐

  1. ES8Object方法-使用说明

    2024-04-29 12:18:01       35 阅读
  2. es6Object.assign

    2024-04-29 12:18:01       35 阅读
  3. React使用 lodash-es 的throttle方法失效

    2024-04-29 12:18:01       34 阅读
  4. [DM8] 序列使用说明

    2024-04-29 12:18:01       56 阅读
  5. typescript常用object方法

    2024-04-29 12:18:01       54 阅读
  6. Objective-C的“description“方法

    2024-04-29 12:18:01       49 阅读
  7. ES6 字符串的方法

    2024-04-29 12:18:01       43 阅读
  8. Kotlinobject关键字的使用

    2024-04-29 12:18:01       60 阅读
  9. 理解 Objective-C `+load` 方法的执行顺序

    2024-04-29 12:18:01       27 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-29 12:18:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-29 12:18:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-29 12:18:01       82 阅读
  4. Python语言-面向对象

    2024-04-29 12:18:01       91 阅读

热门阅读

  1. 双非二本找工作前的准备day13

    2024-04-29 12:18:01       31 阅读
  2. pytorch对音频数据的读取和保存

    2024-04-29 12:18:01       32 阅读
  3. Linux深入学习 - 进程

    2024-04-29 12:18:01       35 阅读
  4. stm32 boot脚设计

    2024-04-29 12:18:01       25 阅读
  5. FreeLearning Golang 译文集翻译完成

    2024-04-29 12:18:01       31 阅读
  6. C++——数据类型笔记

    2024-04-29 12:18:01       23 阅读
  7. python常用库函数

    2024-04-29 12:18:01       23 阅读
  8. HTTP状态码详细解读

    2024-04-29 12:18:01       28 阅读
  9. C语言真题20套

    2024-04-29 12:18:01       26 阅读
  10. Python医院挂号脚本

    2024-04-29 12:18:01       32 阅读
  11. 蓝桥杯每日一题:空调(差分)

    2024-04-29 12:18:01       23 阅读
  12. 学习 Rust 的第五天:了解程序的基本控制流程

    2024-04-29 12:18:01       28 阅读
  13. 冷热数据分离方案

    2024-04-29 12:18:01       30 阅读