"false" VS false / array.flat() / κ°μ²΄ μ§ν₯ νλ‘κ·Έλλ° / prototype / function.call() / array.filter() / && / array.includes() / array.push() / array.map()
javascript-koans νμ΄ μλ£
μ΄μ νμ΄ μ΄μ΄μ
νλ©΄μ λͺ¨λ₯΄κ±°λ ν·κ°λ Έλ λΆλΆ μ 리
(1) var msg = "" + ((3 % 2) === 0);
var msg = "";
msg = msg + ((3 % 2) === 0);
// msg = "" + false;
// ""μ falseλ₯Ό λν κ°μ λ³μ msgμ ν λΉ
console.log(msg);
// "false" (β΅ "" + false === "false")
msg = msg + ((2 % 2) === 0);
// msg = "false" + true;
// "false"μ trueλ₯Ό λν κ°μ λ³μ msgμ ν λΉ
console.log(msg);
// "falsetrue"
(2) var msg = "" + (3 % 2) === 0;
var msg = "";
msg = msg + (3 % 2) === 0;
// msg = "" + 1 === 0;
// ""μ 1μ λν κ°μ΄ 0κ³Ό μΌμΉνλμ§λ₯Ό νλ¨ ν true/false κ°μ msg λ³μμ ν λΉ
console.log(msg);
// false (β΅ "1" !== 0)
msg = msg + (2 % 2) === 0;
// msg = false + 0 === 0;
// falseμ 0μ λν κ°μ΄ 0κ³Ό μΌμΉνλμ§λ₯Ό νλ¨ ν true/false κ°μ msgμ ν λΉ
console.log(msg);
// true (β΅ console.log(false + 0); // 0 )
cf. boolean(λΆλ¦¬μΈ)
console.log(true); // true
console.log(true + 0); // 1
console.log(false); // false
console.log(false + 1); // 0
(1) μ€μ²©λ λ°°μ΄ ννν
var array = [[1, 2], [[3, 4], [5, 6]]];
var flat1 = array.flat();
console.log(flat1);
// [1, 2, [3, 4], [5, 6]]
var flat2 = array.flat(2);
console.log(flat2)
// [1, 2, 3, 4, 5, 6]
(2) λ°°μ΄μ κ΅¬λ© μ κ±°
var array = [1, 2, , 3, 4];
var flat = array.flat();
console.log(flat);
// [1, 2, 3, 4]
AboutInheritance.js νλ€κ° prototype κ°μ λ£κ³ μ΄
μνμ½λ© κ°μ²΄ μ§ν₯ νλ‘κ·Έλλ° κ°μ (1 ~ 12νΈ(7.2)) μ°Έκ³
μλ‘ μ°κ΄λ
λ³μμ ν¨μλ₯Ό
κ·Έλ£Ήν νκ³
μ΄λ¦μ λΆμΈ κ²
thisκ° μν΄ μλ λ©μλ
κ° μν΄ μλ κ°μ²΄λ₯Ό κ°λ¦¬ν΄
μ΄νμ μλ―Έ : μν, 곡ν΅λ λͺ¨μ΅, λ³Έλμ λͺ¨μ΅
μλ°μ€ν¬λ¦½νΈ : νλ‘ν νμ κΈ°λ° μΈμ΄
μμ±μλ₯Ό μ΄μ©ν΄ κ°μ²΄λ₯Ό μμ±ν λλ§λ€ λ©μλκ° λ§λ€μ΄μ Έ λ©λͺ¨λ¦¬μ ν λΉμ΄ λκΈ° λλ¬Έμ λ©λͺ¨λ¦¬ ν¨μ¨μ΄ μ’μ§ μλ€
function Person (name, first, second) {
this.name = name;
this.first = first;
this.second = second;
// μ΄ λΆλΆμ΄ kim, lee λ±μ κ°μ²΄λ€μ μμ±ν λλ§λ€ μλ‘ λ§λ€μ΄μ Έ λ©λͺ¨λ¦¬μ ν λΉλλ€
this.sum = function () {
return this.first + this.second;
}
}
var kim = new Person('kim', 10, 20);
var lee = new Person('Lee', 10, 10);
console.log(kim.sum()); // 30
console.log(lee.sum()); // 20
ν λ²λ§ μ μν΄ λμΌλ©΄ λκΈ° λλ¬Έμ λ©λͺ¨λ¦¬κ° λΉν¨μ¨μ μΌλ‘ λλΉλμ§ μλλ€
(1) μμ±μλ₯Ό μ΄μ©ν΄ μμ±ν λͺ¨λ κ°μ²΄λ€μ λ©μλλ₯Ό μ μνλ λ°©λ²
// κ°μ²΄μ π‘μμ±π‘μ κ°μ²΄ μμ±μ ν¨μμ μ μν΄λκ³
function Person (name, first, second) {
this.name = name;
this.first = first;
this.second = second;
}
// ν¨μ¨μ±μ μν΄ κ°μ²΄μ π‘λ©μλπ‘λ prototypeμ μ΄μ©ν΄ λ°λ‘ μ μν κ²
Person.prototype.sum = function () {
return this.first + this.second;
}
var kim = new Person('kim', 10, 20);
var lee = new Person('Lee', 10, 10);
console.log(kim.sum()); // 30
console.log(lee.sum()); // 20
(2) μμ±μλ₯Ό μ΄μ©ν΄ μμ±ν κ°μ²΄λ€ μ€ μ΄λ ν κ°μ²΄μ λ©μλλ§ λ°κΎΈλ λ°©λ²
function Person (name, first, second) {
this.name = name;
this.first = first;
this.second = second;
}
Person.prototype.sum = function () {
return this.first + this.second;
}
var kim = new Person('kim', 10, 20);
var lee = new Person('Lee', 10, 10);
// μΆκ°ν μ½λ
kim.sum = function () {
return 'modified: ' + (this.first + this.second);
}
console.log(kim.sum()); // 'modified: 30'
console.log(lee.sum()); // 20
μλ°μ€ν¬λ¦½νΈκ° kim.sum()μ κ°μ ꡬν΄μ£Όλ κ³Όμ
β μ΄λ₯Ό νλ‘ν νμ
체μΈ
μ΄λΌκ³ νλ€
β μ΄λ₯Ό ν΅ν΄ λͺ¨λ κ°μ²΄λ κ·Έ κ°μ²΄μ μμ±μμ prototype μμ±μ μ μλ μμ±κ³Ό λ©μλλ€μ μμν μ μλ€
λ€μ λμμμ
ν¨μ(Function)μ ν¨μμ call() λ©μλ μ°Έκ³
var koGreeting = {
greeting : "μλ
",
name : "νκΈΈλ",
sayHello: function() {
return this.greeting + ", " + this.name;
}
};
var enGreeting = {
greeting: "Hello",
name: "HongKilDong"
};
console.log(koGreeting.sayHello());
// μλ
, νκΈΈλ
console.log(koGreeting.sayHello.call(enGreeting));
// Hello, HongKilDong
λ§μ§λ§ μ€ μ½λμ μλ―Έ
call() λ©μλλ koGreeting κ°μ²΄ μμ sayHello() λ©μλκ° (koGreeting κ°μ²΄κ° μλλΌ) call() λ©μλμ 첫 λ²μ§Έ μΈμλ‘ μ£Όμ΄μ§ enGreeting κ°μ²΄μ λ©μλλ‘ λμνλλ‘ ν΄μ€λ€
μ μ΄.λ ΅.λ€.
var products = [
{ name: "Sonoma", ingredients: ["artichoke", "sundried tomatoes", "mushrooms"], containsNuts: false },
{ name: "Pizza Primavera", ingredients: ["roma", "sundried tomatoes", "goats cheese", "rosemary"], containsNuts: false },
{ name: "South Of The Border", ingredients: ["black beans", "jalapenos", "mushrooms"], containsNuts: false },
{ name: "Blue Moon", ingredients: ["blue cheese", "garlic", "walnuts"], containsNuts: true },
{ name: "Taste Of Athens", ingredients: ["spinach", "kalamata olives", "sesame seeds"], containsNuts: true }
];
var productsICanEat = products.filter(function (obj) {
return (obj.containsNuts === false) && (!obj.ingredients.includes('mushrooms'));
});
console.log(productsICanEat);
// [ { name: "Pizza Primavera", ingredients: ["roma", "sundried tomatoes", "goats cheese", "rosemary"], containsNuts: false } ]
filter() λ©μλμ μΈμλ‘ λ€μ΄μ€λ ν¨μμ 맀κ°λ³μ obj
λ filter() λ©μλλ₯Ό νΈμΆν λ°°μ΄ productsμ μμ
λ₯Ό μλ―Ένλ€
filter() λ©μλλ, μ£Όμ΄μ§ ν¨μμ ν
μ€νΈλ₯Ό ν΅κ³Όνλ(μ£Όμ΄μ§ ν¨μμ κ²°κ³Όκ° trueμΈ κ²½μ°μ
) λͺ¨λ μμλ€μ λͺ¨μ μλ‘μ΄ λ°°μ΄λ‘ λ°ννλ€
(νΌμ°μ°μ) && (νΌμ°μ°μ)
obj.containsNuts === false
β νΌμ°μ°μ 1 (true/false)
!obj.ingredients.includes('mushrooms')
νΌμ°μ°μ 2 (true/false)
νΌμ°μ°μ 1κ³Ό 2κ° λͺ¨λ tureλΌλ©΄
, && μ°μ°μμ μν΄ trueκ° λ°νλλ€
λ°λΌμ, products.filter(ν¨μ)
λ νΌμ°μ°μ 1κ³Ό 2κ° λͺ¨λ trueκ° λλ objλ€μ μ°Ύμ μλ‘μ΄ λ°°μ΄λ‘ λ°ννλ€
// λΉ λ°°μ΄ array μμ±
var array = [];
// λ°°μ΄ arrayμ 1000 λ―Έλ§μ 3 λλ 5μ λ°°μμΈ μμ°μλ€μ μμλ‘ μ½μ
for (var i = 1; i < 1000; i++) {
if (i % 3 === 0 || i % 5 === 0) {
array.push(i);
}
}
// μμλ€μ κ°μ λ€ λνκΈ° μν΄ reduce() μ΄μ©
var answer = array.reduce((prev, curr) => prev + curr);
console.log(answer);
// 233168
ꡬνκΈ΄ νλλ° λκ° μ’ μ΄μν κ±° κ°λ€
products λ°°μ΄μ κ° μμ(μ¦, κ° κ°μ²΄μ ingredients μμ±μ κ°λ€(μ¦, ingredients λ°°μ΄μ μμλ€))μ λ½μμ μλ‘μ΄ λ°°μ΄λ‘ λ§λ¦
β map μ΄μ©
κ·Έ λ§λ λ°°μ΄λ€μ νλμ λ°°μ΄μ λ£κΈ° μν΄ ννν μμ
β flat() μ΄μ©
mushroomsλ§ κ³¨λΌλ β filter() μ΄μ©
lengthλ₯Ό ꡬν¨
var products = [
{ name: "Sonoma", ingredients: ["artichoke", "sundried tomatoes", "mushrooms"], containsNuts: false },
{ name: "Pizza Primavera", ingredients: ["roma", "sundried tomatoes", "goats cheese", "rosemary"], containsNuts: false },
{ name: "South Of The Border", ingredients: ["black beans", "jalapenos", "mushrooms"], containsNuts: false },
{ name: "Blue Moon", ingredients: ["blue cheese", "garlic", "walnuts"], containsNuts: true },
{ name: "Taste Of Athens", ingredients: ["spinach", "kalamata olives", "sesame seeds"], containsNuts: true }
];
// 1. μ 리λ₯Ό μν΄ λ©μλλ₯Ό μΈ λλ§λ€ λ³μμ ν λΉν λ²μ
var allIngredients = products.map(function (obj) {
return obj.ingredients;
});
var flat = allIngredients.flat();
var mushrooms = flat.filter(function (ingredient) {
return ingredient === 'mushrooms';
});
console.log(mushrooms.length); // 2
// 2. ν λ²μ μ§§κ² μ€μΈ λ²μ
var ingredientCount = products.map(function (obj) {
return obj.ingredients;
})
.flat()
.filter(function (ingredient) {
return ingredient === 'mushrooms';
}).length;
console.log(ingredientCount); // 2
μλ μ¬μ μ μλ λ μ€μ μ½λκ° μ£Όμ΄μ Έ μμλλ° λ΄κ° λ§λ€λ€ 보λ μ΄κ²λ€μ΄ λ°°μ λμλ€. μ¬μ€ μ΄ μ½λλ€μ΄ μ μ΄ν΄κ° κ°μ§ μλλ€.
var ingredientCount = { "{ingredient name}": 0 };
console.log(ingredientCount['mushrooms']);
κ²μν΄λ³΄κ³ μ΄
μ²μλΆν° mushroomμ΄ λ€μ΄κ° κ°μλ₯Ό ꡬνλ κ² μλλΌ μ΄λ€ μ¬λ£λΌλ λͺ κ°κ° λ€μ΄κ°λμ§ κ΅¬ν μ μλλ‘ μ½λλ₯Ό μ§μΌ νλ κ²μ΄μλ€.
νμ§λ§, μμ λ μ½λλ€μ λΉλ‘―ν΄ μ¬μ ν μ΄ν΄κ° μ κ°λ€.