Object.entries/Object.values/Object.keys
Object.keys 遍历 Object 的key
const family = {
father: "Jonathan Kent",
mother: "Martha Kent",
son: "Clark Kent",
}
Object.keys(family);
// ["father", "mother", "son"]Object.values 遍历 Object 的值
Object.values(family);
// ["Jonathan Kent", "Martha Kent", "Clark Kent"]Object.entries 返回一个 [ key, values ] 形式的数组。 数组的顺序和 Object 如何定义的没有联系。
const object1 = {
a: 'somestring',
b: 42
};
for (let [key, value] of Object.entries(object1)) {
console.log(`${key}: ${value}`);
}
// expected output:
// "a: somestring"
// "b: 42"
// order is not guaranteed对于 Object.entries 输入为非对象,会自动转换为对象再操作
最后更新于
这有帮助吗?