๐ซ ์ฒ์๋ถํฐ ๊ผผ๊ผผํ ๋ค์ ๊ณต๋ถํ๋ javascript object
let user = {
name: "John",
age: 30,
};
const user = {
name: "John"
};
user.name = "Pete"; // (*)
alert(user.name); // Pete
let fruit = prompt("Which fruit to buy?", "apple");
let bag = {
[fruit]: 5,
};
alert( bag.apple ); // 5
// complex expression
let bag = {
[fruit + 'Computers']: 5 // bag.appleComputers = 5
};
let obj = {
test: undefined
};
alert( obj.test ); // undefined
alert( "test" in obj ); // true
// Integer
let codes = {
"49": "Germany",
"41": "Switzerland",
"44": "Great Britain",
"1": "USA"
};
for (let code in codes) {
alert(code); // 1, 41, 44, 49
}
// If you want integer not sorted
let codes = {
"+49": "Germany",
"+41": "Switzerland",
"+44": "Great Britain",
"+1": "USA"
};
// Primitive
let message = "Hello!";
let phrase = message;
// Object
let user = { name: "John" };
let admin = user; // copy the reference. user and admin share the same an object
admin.name = 'Pete';
alert(user.name); // Pete
Object.assign()
Object.assign(dest, [src1, src2, src3...])
src1, ..., srcN
into the target dest.let clone = Object.assign({}, user);
const original = {
name: 'Fiesta',
car: {
color: 'blue',
},
}
const copied = Object.assign({}, original)
original.name = 'Focus'
original.car.color = 'yellow'
copied.name //Fiesta
copied.car.color //yellow
Object.assign()
const copied = { ...original }
Object๋ฅผ array๋ก ๋ฐ๊พธ์ด ์์ ์ ์คํํ๊ณ ์ถ์ ๋ (map, filter๋ฑ)
Object.keys(obj)
โ returns an array of keys.Object.values(obj)
โ returns an array of values.Object.entries(obj)
โ returns an array of [key, value] pairs.let prices = {
banana: 1,
orange: 2,
meat: 4,
};
let doublePrices = Object.fromEntries(
Object.entries(prices).map(([key, value]) => [key, value * 2])
);
alert(doublePrices.meat); // 8