Baseline Widely available
Object.entries()
ë©ìëë for...in
ì ê°ì ììë¡ ì£¼ì´ì§ ê°ì²´ ìì²´ì enumerable ìì± [key, value]
ìì ë°°ì´ì ë°íí©ëë¤. (for-in
루íê° ë¤ë¥¸ì ì íë¡í íì
ì²´ì¸ì ìì±ë ì´ê±°íë¤ë ì ì
ëë¤).
Object.entries()
ì ìí´ ë°íë ë°°ì´(array)ì ììë ê°ì²´ê° ì ìë ë°©ë²ê³¼ ê´ë ¨ì´ ììµëë¤. ë°°ì´ ììê° ì¸ ê³³ì´ ìë¤ë©´, ë¤ìê³¼ ê°ì´ ì ë ¬ì 먼ì íìë ê²ì´ ì¢ìµëë¤ Object.entries(obj).sort((a, b) => b[0].localeCompare(a[0]));
.
const object1 = {
a: "somestring",
b: 42,
};
for (const [key, value] of Object.entries(object1)) {
console.log(`${key}: ${value}`);
}
// Expected output:
// "a: somestring"
// "b: 42"
Syntax Parameters
obj
ê°ì²´ ìì²´ì ì´ê±° ê°ë¥í 문ìì´ í¤ë¥¼ ê°ì§ ìì± [key, value]
ìì´ ë°íëë ê°ì²´ì
ëë¤.
ì§ì ë ê°ì²´ ìì²´ì ì´ê±° ê°ë¥í 문ììì± [key, value]
ìì ë°°ì´ì
ëë¤.
Object.entries()
ë object
ì ì§ì ìë enumerable ìì± [key, value]
ìì í´ë¹íë ë°°ì´ì ë°íí©ëë¤. ìì±ì ììë ê°ì²´ì ìì± ê°ì ìëì¼ë¡ ë°ë³µíì¬ ì£¼ì´ì§ ììì ëì¼í©ëë¤.
기본ì ì¼ë¡ ì§ìíì§ ìë ì´ì íê²½ìì í¸í ê°ë¥í Object.entries
ì§ìì ì¶ê°íë ¤ë©´ tc39/proposal-object-values-entriesì Object.entriesì ë°ëª¨ 구íì ì°¾ì ì ììµëë¤ (IEì ëí ì§ìì´ íìíì§ ìì ê²½ì°) , es-shims/Object.entries ì ì¥ìììë polyfillì ì¬ì©íê±°ë ìëì ëì´ë polyfillì ê°ë¨íê² ë°°ì¹ í ì ììµëë¤.
if (!Object.entries)
Object.entries = function (obj) {
var ownProps = Object.keys(obj),
i = ownProps.length,
resArray = new Array(i); // preallocate the Array
while (i--) resArray[i] = [ownProps[i], obj[ownProps[i]]];
return resArray;
};
For the above polyfill code snippet, if you need support for IE < 9, then you will also need an Object.keys polyfill (such as the one found on the Object.keys
page).
ìì polyfill ì½ë ì¤ ëí«ì ê²½ì° Internet Explorer (9ë²ì ì´ì )를 ì§ìí´ì¼íë ê²½ì° Object.keys polyfill ( Object.keys
íì´ì§ì ìë ê²ê³¼ ê°ì)ë íìí©ëë¤.
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'] ]
// returns an empty array for any primitive type, since primitives have no own properties
console.log(Object.entries(100)); // [ ]
// 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"
});
Converting an Object
to a Map
new Map()
ìì±ìë ë°ë³µ ê°ë¥í í목ì íì©í©ëë¤. Object.entries
를 ì¬ì©íë©´ Object
ìì Map
ë¡ ì½ê² ë³í í ì ììµëë¤.
const obj = { foo: "bar", baz: 42 };
const map = new Map(Object.entries(obj));
console.log(map); // Map { foo: "bar", baz: 42 }
Iterating through an Object
Array Destructuringì ì¬ì©íë©´ ê°ì²´ë¥¼ ì½ê² ë°ë³µ í ì ììµëë¤.
const obj = { foo: "bar", baz: 42 };
Object.entries(obj).forEach(([key, value]) => console.log(`${key}: ${value}`)); // "foo: bar", "baz: 42"
ëª
ì¸ì ë¸ë¼ì°ì í¸íì± See also
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4