const actors = [{ name: 'κΉκ³ μ' }, { name: 'κ³ μ€μ ' }];
console.log(actors); // Array[Object, Object]
console.log(actors[0]); // Object {name:"κΉκ³ μ"}
console.log(actors[1]); // Object {name:"κ³ μ€μ "}
actors.push({ name: 'μ μΈκ²½' });
let numbers = [10, 20, 30, 40, 50];
for (let number of numbers) {
console.log(number); // 10 20 30 40 50
}
const singer = {
name: 'λ°μ±μ',
group: 'BLACKPINK',
age: 24
};
//Object ν¨μ μ¬μ©
console.log(Object.entries(singer)); // [["name", "λ°μ±μ"], ["group", "BLACKPINK"], ["age" , 24]]
console.log(Object.keys(doggy)); // ["name", "group", "age" ]
console.log(Object.values(doggy)); // ["λ°μ±μ", "BLACKPINK", 24]
// for...in μ¬μ©
for (let key in singer) {
console.log(`${key}: ${singer[key]}`);
// name: λ°μ±μ
// group: BLACKPINK
// age: 24
}
const actors = ['κΉκ³ μ', 'κ³ μ€μ ', 'μ μΈκ²½', 'νμν¬'];
// for λ°λ³΅λ¬Έ μ¬μ©
for (let i = 0; i < actors.length; i++) {
console.log(actors[i]);
}
// forEach μ¬μ©
actors.forEach(actor => {
console.log(actor);
});
// κ²°κ³Ό λμΌ
const array = [1, 2, 3, 4, 5, 6, 7, 8];
// μ κ³±ν΄μ μλ‘μ΄ λ°°μ΄ μμ±
const squared = []; // λ³νν¨μ
// for λ°λ³΅λ¬Έ μ¬μ©
for (let i = 0; i < array.length; i++) {
squared.push(array[i] * array[i]);
}
// forEach μ¬μ©
array.forEach(n => {
squared.push(n * n);
});
// map μ¬μ© 1
const square = n => n * n; // λ³νν¨μ μ΄λ¦ μ μΈ
const squared = array.map(square); // λ³νν¨μ μ λ¬
// map μ¬μ© 2
const squared = array.map(n => n * n); // λ³νν¨μ μ΄λ¦ μ μΈ X
// κ²°κ³Ό λμΌ
console.log(squared); // [1, 4, 9, 16, 25, 36, 49, 64];
const actors = ['κΉκ³ μ', 'κ³ μ€μ ', 'μ μΈκ²½', 'νμν¬'];
const index = actors.indexOf('μ μΈκ²½');
console.log(index); // 2
const todos = [
{
id: 1,
text: 'λ³μ',
done: true
},
{
id: 2,
text: 'ν¨μ',
done: true
},
{
id: 3,
text: 'λ°λ³΅λ¬Έ',
done: true
},
{
id: 4,
text: 'λ°°μ΄ λ΄μ₯ν¨μ',
done: false
}
];
const index = todos.findIndex(todo => todo.id === 3);
console.log(index); // 2
...
const todo = todos.find(todo => todo.id === 3);
console.log(todo);
// {id: 3, text: "λ°λ³΅λ¬Έ", done: true}
...
const tasksNotDone = todos.filter(todo => todo.done === false);
// const tasksNotDone = todos.filter(todo => !todo.done);
console.log(tasksNotDone);
// [
{
id: 4,
text: 'λ°°μ΄ λ΄μ₯ ν¨μ',
done: false
}
];
const numbers = [10, 20, 30, 40];
const index = numbers.indexOf(30);
numbers.splice(index, 1);
console.log(numbers); // [10, 20, 40]
const numbers = [10, 20, 30, 40];
const sliced = numbers.slice(0, 2); // 0λΆν° μμν΄μ 2μ κΉμ§
console.log(sliced); // [10, 20]
console.log(numbers); // [10, 20, 30, 40]
const numbers = [10, 20, 30, 40];
const value = numbers.shift();
console.log(value); // 10
console.log(numbers); // [20, 30, 40]
const numbers = [10, 20, 30, 40];
const value = numbers.pop();
console.log(value); // 40
console.log(numbers); // [10, 20, 30]
const numbers = [10, 20, 30, 40];
numbers.unshift(5);
console.log(numbers); // [5, 10, 20, 30, 40]
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const concated = arr1.concat(arr2);
console.log(concated); // [1, 2, 3, 4, 5, 6]
const array = [1, 2, 3, 4, 5];
console.log(array.join()); // 1,2,3,4,5
console.log(array.join(' ')); // 1 2 3 4 5
console.log(array.join(', ')); // 1, 2, 3, 4, 5
const numbers = [1, 2, 3, 4, 5];
// forEach μ¬μ©
let sum = 0;
numbers.forEach(n => {
sum += n;
});
// reduce μ¬μ©
const numbers = [1, 2, 3, 4, 5];
let sum = array.reduce((accumulator, current) => accumulator + current, 0);
console.log(sum);
const numbers = [1, 2, 3, 4, 5];
let sum = numbers.reduce((accumulator, current) => {
console.log({ accumulator, current });
// Object {accumulator: 0, current: 1} // μ΄κΉκ° 0μ΄κΈ° λλ¬Έμ accumulator: 0
// Object {accumulator: 1, current: 2} // accumulator: 0 + 1 = 1
// Object {accumulator: 3, current: 3} // accumulator: 1 + 2 = 3
// Object {accumulator: 6, current: 4}
// Object {accumulator: 10, current: 5}
return accumulator + current;
}, 0);
console.log(sum); // 15
let average = numbers.reduce((accumulator, current, index, array) => {
if (index === array.length - 1) {
return (accumulator + current) / array.length;
}
return accumulator + current;
}, 0);
console.log(average); // 3
function Animal(type, name, sound) {
this.type = type;
this.name = name;
this.sound = sound;
this.say = function() {
console.log(this.sound);
};
}
const dog = new Animal('κ°', 'μ°μ²΄', 'λ©λ©');
const cat = new Animal('κ³ μμ΄', 'λ²μ΄', 'μΌμΉ');
dog.say(); // λ©λ©
cat.say(); // μΌμΉ
function Animal(type, name, sound) {
this.type = type;
this.name = name;
this.sound = sound;
}
Animal.prototype.say = function() { // νλ‘ν νμ
console.log(this.sound);
};
Animal.prototype.sharedValue = 1; // νλ‘ν νμ
const dog = new Animal('κ°', 'μ°μ²΄', 'λ©λ©');
const cat = new Animal('κ³ μμ΄', 'λ²', 'μΌμΉ');
dog.say(); // λ©λ©
cat.say(); // μΌμΉ
console.log(dog.sharedValue); // 1
console.log(cat.sharedValue); // 1
Animal.prototype.say = function() {
console.log(this.sound);
};
Animal.prototype.sharedValue = 1;
function Dog(name, sound) {
Animal.call(this, 'κ°', name, sound); // Animal.call νΈμΆ
}
Dog.prototype = Animal.prototype; // μμ
function Cat(name, sound) {
Animal.call(this, 'κ³ μμ΄', name, sound); // Animal.call νΈμΆ
}
Cat.prototype = Animal.prototype; // μμ
const dog = new Dog('μ°μ²΄', 'λ©λ©');
const cat = new Cat('λ²', 'μΌμΉ');
dog.say();
cat.say();
class Animal {
constructor(type, name, sound) { // constructor μ¬μ©
this.type = type;
this.name = name;
this.sound = sound;
}
say() { // λ©μλ
console.log(this.sound);
}
}
const dog = new Animal('κ°', 'μ°μ²΄', 'λ©λ©');
const cat = new Animal('κ³ μμ΄', 'λ²', 'μΌμΉ');
dog.say(); // λ©λ©
cat.say(); // μΌμΉ
class Animal {
constructor(type, name, sound) {
this.type = type;
this.name = name;
this.sound = sound;
}
say() {
console.log(this.sound);
}
}
class Dog extends Animal { // μμ, extends ν€μλ μ¬μ©
constructor(name, sound) {
super('κ°', name, sound); // super() ν¨μ μ¬μ©
}
}
class Cat extends Animal {
constructor(name, sound) {
super('κ³ μμ΄', name, sound);
}
}
const dog = new Dog('μ°μ²΄', 'λ©λ©');
const dog2 = new Dog('μ£½μ체', 'μμ');
const cat = new Cat('λ²', 'μΌμΉ');
const cat2 = new Cat('λλΉ', 'λμΉ');
dog.say(); // λ©λ©
dog2.say(); // μμ
cat.say(); // μΌμΉ
cat2.say(); // λμΉ
Reference
벨λ‘νΌνΈμ ν¨κ»νλ λͺ¨λ μλ°μ€ν¬λ¦½νΈ