From a55b575e8089ee6cab7c5c262a7e6db55d0e34d6 Mon Sep 17 00:00:00 2001 From: Florian Merz Date: Thu, 11 Feb 2021 14:46:50 +0100 Subject: unslug es: move --- .../global_objects/object/entries/index.html | 161 +++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 files/es/web/javascript/reference/global_objects/object/entries/index.html (limited to 'files/es/web/javascript/reference/global_objects/object/entries/index.html') diff --git a/files/es/web/javascript/reference/global_objects/object/entries/index.html b/files/es/web/javascript/reference/global_objects/object/entries/index.html new file mode 100644 index 0000000000..98aff1178a --- /dev/null +++ b/files/es/web/javascript/reference/global_objects/object/entries/index.html @@ -0,0 +1,161 @@ +--- +title: Object.entries() +slug: Web/JavaScript/Referencia/Objetos_globales/Object/entries +translation_of: Web/JavaScript/Reference/Global_Objects/Object/entries +--- +
{{JSRef}}
+ +

El método Object.entries() devuelve una matriz de pares propios de una propiedad enumerable [key, value] de un objeto dado, en el mismo orden que es proporcionado por {{jsxref("Sentencias/for...in", "for...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 upon object. The ordering of the properties is the same as that given by looping over the property values of the object manually.

+ +

Ejemplos

+ +
var obj = { foo: 'bar', baz: 42 };
+console.log(Object.entries(obj)); // [ ['foo', 'bar'], ['baz', 42] ]
+
+// array like object
+var 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
+var 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 enumerable
+var 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 object
+console.log(Object.entries('foo')); // [ ['0', 'f'], ['1', 'o'], ['2', 'o'] ]
+
+// iterate through key-value gracefully
+var 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 extras
+Object.entries(obj).forEach(([key, value]) => {
+    console.log(key + ' ' + value); // "a 5", "b 7", "c 9"
+});
+
+ +

Converting an Object to a Map

+ +

The {{jsxref("Map", "new Map()")}} constructor accepts an iterable of entries. With Object.entries, you can easily convert from {{jsxref("Object")}} to {{jsxref("Map")}}:

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

Polyfill

+ +

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

+ +

Specifications

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ESDraft', '#sec-object.entries', 'Object.entries')}}{{Spec2('ESDraft')}}Initial definition.
{{SpecName('ES8', '#sec-object.entries', 'Object.entries')}}{{Spec2('ES8')}}
+ +

Browser compatibility

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatChrome(54)}}{{CompatVersionUnknown}}{{CompatGeckoDesktop(47)}}{{CompatNo}}{{CompatNo}}{{CompatSafari(10.1)}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroid WebviewChrome for AndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatChrome(54)}}{{CompatChrome(54)}}{{CompatVersionUnknown}}{{CompatGeckoMobile(47)}}{{CompatNo}}{{CompatNo}}{{CompatNo}}
+
+ +

See also

+ + -- cgit v1.2.3-54-g00ecf