728x90
๐ Key:Valueํํ์ธ Object์ map ํจ์ ์ฌ์ฉํ๊ธฐ
๋ค์ ๊ณผ๋ชฉ:์ ์
ํํ์ธ Object
์์ map
ํจ์๋ฅผ ์ฌ์ฉํ ๋ 3๊ฐ์ง ๋ฐฉ๋ฒ์ด ์๋ค.
- Object.entries:
array
ํํ๋ก[key, value]
๋ฅผ ๋ฐํํ๋ค. ์ฃผ์ ํ ์ ์ ๋ฐํ ์ ๊ฐ์ฒด์ ์์๋ฅผ ๋ณด์ฅํ์ง ์์ผ๋ฏ๋ก ์ ๋ ฌ์ ๋จผ์ ํ๊ณ ๋์ ์ฌ์ฉํ๋ ๊ฒ์ ๊ถ์ฅํ๋ค. (Object.entries(obj).sort((a, b) => b[0].localeCompare(a[0]));
, ์ถ์ฒ: MDN) - Object.keys:
key
๋ง ๋ฐํํ๋ค. - Object.values:
value
๋ง ๋ฐํํ๋ค.
const subjects = { math: 90, english: 100, science: 80 };
// idx๋ index ๋ฒํธ๋ฅผ ๋ฐํํ๊ณ ๋ฐ๋ก ๋ช
์ํ์ง ์์๋ค.
const getEntries = Object.entries(subjects).map((entrie, idx) => {
return console.log(entrie, idx);
});
๐๐ฝ ['math', 90], ['english', 100], ['science', 80]
const getKeys = Object.keys(subjects).map((entrie, idx) => {
return console.log(entrie, idx);
});
๐๐ฝ 'math', 'english', 'science'
const getValues = Object.values(subjects).map((entrie, idx) => {
return console.log(entrie, idx);
});
๐๐ฝ 90, 100, 80
๋ฐ์ํ
๋๊ธ