Movatterモバイル変換


[0]ホーム

URL:


  1. Tecnología web para desarrolladores
  2. JavaScript
  3. Referencia de JavaScript
  4. Objetos globales
  5. Object
  6. Object.entries()

Esta página ha sido traducida del inglés por la comunidad.Aprende más y únete a la comunidad de MDN Web Docs.

View in EnglishAlways switch to English

Object.entries()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨marzo de 2017⁩.

El métodoObject.entries() devuelve una matriz de pares propios de una propiedad enumerable [key, value] de un objeto dado, en el mismo orden que es proporcionado porfor...in (La diferencia es que un bucle for-in enumera las propiedades en la cadena de prototipos).

Sintaxis

Object.entries(obj)

Parámetros

obj

The object whose enumerable own property[key, value] pairs are to be returned.

Valor de retorno

An array of the given object's own enumerable property[key, value] pairs.

Descripción

Object.entries() returns an array whose elements are arrays corresponding to the enumerable property[key, value] pairs found directly uponobject. The ordering of the properties is the same as that given by looping over the property values of the object manually.

Ejemplos

js
var obj = { foo: "bar", baz: 42 };console.log(Object.entries(obj)); // [ ['foo', 'bar'], ['baz', 42] ]// array like objectvar obj = { 0: "a", 1: "b", 2: "c" };console.log(Object.entries(obj)); // [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ]// array like object with random key orderingvar an_obj = { 100: "a", 2: "b", 7: "c" };console.log(Object.entries(an_obj)); // [ ['2', 'b'], ['7', 'c'], ['100', 'a'] ]// getFoo is property which isn't enumerablevar my_obj = Object.create(  {},  {    getFoo: {      value: function () {        return this.foo;      },    },  },);my_obj.foo = "bar";console.log(Object.entries(my_obj)); // [ ['foo', 'bar'] ]// non-object argument will be coerced to an objectconsole.log(Object.entries("foo")); // [ ['0', 'f'], ['1', 'o'], ['2', 'o'] ]// iterate through key-value gracefullyvar obj = { a: 5, b: 7, c: 9 };for (var [key, value] of Object.entries(obj)) {  console.log(key + " " + value); // "a 5", "b 7", "c 9"}// Or, using array extrasObject.entries(obj).forEach(([key, value]) => {  console.log(key + " " + value); // "a 5", "b 7", "c 9"});

Converting anObject to aMap

Thenew Map() constructor accepts an iterable ofentries. WithObject.entries, you can easily convert fromObject toMap:

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

Polyfill

To add compatibleObject.entries support in older environments that do not natively support it, you can find a Polyfill in thetc39/proposal-object-values-entries or in thees-shims/Object.entries repositories.

Especificaciones

Specification
ECMAScript® 2026 Language Specification
# sec-object.entries

Compatibilidad con navegadores

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp