Object.entries()

Object.entries() 静态方法返回给定对象自己的可枚举字符串键控属性键值对的数组。

¥The Object.entries() static method returns an array of a given object's own enumerable string-keyed property key-value pairs.

Try it

语法

¥Syntax

js
Object.entries(obj)

参数

¥Parameters

obj

一个东西。

返回值

¥Return value

给定对象自己的可枚举字符串键控属性键值对的数组。每个键值对都是一个包含两个元素的数组:第一个元素是属性键(始终是字符串),第二个元素是属性值。

¥An array of the given object's own enumerable string-keyed property key-value pairs. Each key-value pair is an array with two elements: the first element is the property key (which is always a string), and the second element is the property value.

描述

¥Description

Object.entries() 返回一个数组,其元素是与直接在 object 上找到的可枚举字符串键控属性键值对相对应的数组。这与使用 for...in 循环进行迭代相同,只是 for...in 循环也会枚举原型链中的属性。Object.entries() 返回的数组的顺序与 for...in 循环提供的数组的顺序相同。

¥Object.entries() returns an array whose elements are arrays corresponding to the enumerable string-keyed property key-value pairs found directly upon object. This is the same as iterating with a for...in loop, except that a for...in loop enumerates properties in the prototype chain as well. The order of the array returned by Object.entries() is the same as that provided by a for...in loop.

如果你只需要属性键,请改用 Object.keys()。如果你只需要属性值,请改用 Object.values()

¥If you only need the property keys, use Object.keys() instead. If you only need the property values, use Object.values() instead.

示例

¥Examples

使用 Object.entries()

¥Using Object.entries()

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

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

const randomKeyOrder = { 100: "a", 2: "b", 7: "c" };
console.log(Object.entries(randomKeyOrder)); // [ ['2', 'b'], ['7', 'c'], ['100', 'a'] ]

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

在基元上使用 Object.entries()

¥Using Object.entries() on primitives

非对象参数是 强制对象undefinednull 不能被强制为对象并预先抛出 TypeError。只有字符串可以拥有自己的可枚举属性,而所有其他原语都返回空数组。

¥Non-object arguments are coerced to objects. undefined and null cannot be coerced to objects and throw a TypeError upfront. Only strings may have own enumerable properties, while all other primitives return an empty array.

js
// Strings have indices as enumerable own properties
console.log(Object.entries("foo")); // [ ['0', 'f'], ['1', 'o'], ['2', 'o'] ]

// Other primitives except undefined and null have no own properties
console.log(Object.entries(100)); // []

将对象转换为映射

¥Converting an Object to a Map

Map() 构造函数接受 entries 的可迭代对象。使用 Object.entries,你可以轻松地从 Object 转换为 Map

¥The Map() constructor accepts an iterable of entries. With Object.entries, you can easily convert from Object to Map:

js
const obj = { foo: "bar", baz: 42 };
const map = new Map(Object.entries(obj));
console.log(map); // Map(2) {"foo" => "bar", "baz" => 42}

迭代对象

¥Iterating through an Object

使用 数组解构,你可以轻松地迭代对象。

¥Using array destructuring, you can iterate through objects easily.

js
// Using for...of loop
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"
}

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

规范

Specification
ECMAScript Language Specification
# sec-object.entries

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看