let numbers = [1, 2, 3, 4, 5, 6];
// ์๋ณธ ๋ฐฐ์ด์ด ๋ณํฉ๋๋ค!! ์ฃผ์ํ์ธ์!!
// ๋ง์ง๋ง์ ์ถ๊ฐ push
numbers.push(7);
console.log(numbers);
// ์ฒ์์ ์ถ๊ฐ push
numbers.unshift(0);
console.log(numbers);
// ๋ง์ง๋ง ์์ ์ญ์ & ๋ฐํ pop
console.log(numbers.pop());
// ์ฒซ ์์ ์ญ์ & ๋ฐํ shift
console.log(numbers.shift());
const numbers1 = [1, 2, 3, 4];
const numbers2 = [1, 2, 3, 4];
console.log(numbers1.splice(0, 2));
console.log(numbers2.splice(2, 1, '77'));
// [3, 4];
// [1, 2, 77, 4];
let numbers = [1, 2, 3, 4, 5, 6];
let fruits = ["์ฌ๊ณผ", "๋ฐ๋๋", "์๋ฐ", "ํฌ๋", "ํ์ธ์ ํ"];
let numbersLength = numbers.length;
let fruitsLength = fruits.length;
console.log("numbers ๋ฐฐ์ด์ ๊ธธ์ด๋", numbersLength); //6
console.log("fruits ๋ฐฐ์ด์ ๊ธธ์ด๋", fruitsLength); //5
let numbers = [1, 2, 3, 4];
let fruits = ["์ฌ๊ณผ", "๋ธ๊ธฐ", "์๋ฐ"];
console.log(numbers.concat(fruits));
console.log(numbers);
console.log(fruits);
let numbers = [1, 2, 3, 4, 5, 6];
let fruits = ["์ฌ๊ณผ", "๋ฐ๋๋", "์๋ฐ", "ํฌ๋", "ํ์ธ์ ํ"];for (let i = 0; i < numbers.length; i++) {
console.log(numbers[i]);
}
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
(์ฐธ๊ณ ๋ก i๋ ํด๋น ํจ์์์๋ง ์ฌ์ฉ๋๊ณ ๋์ด๊ธฐ ๋๋ฌธ์ ํจ์๋ง ๋ค๋ฅด๋ค๋ฉด ๋์ผํ๊ฒ i์ฌ์ฉ ๊ฐ๋ฅ)
let numbers = [1, 2, 3, 4, 5, 6];
let fruits = ["์ฌ๊ณผ", "๋ฐ๋๋", "์๋ฐ", "ํฌ๋", "ํ์ธ์ ํ"];
let numbersLength = numbers.length;
let fruitsLength = fruits.length;
for (let number of numbers) {
console.log(number);
}
for (let fruit of fruits) {
console.log(fruit);
}
let numbers = [1, 2, 3, 4, 5, 6];
let fruits = ["์ฌ๊ณผ", "๋ฐ๋๋", "์๋ฐ", "ํฌ๋", "ํ์ธ์ ํ"];
numbers.forEach(function (number, index, array) {
console.log(number, index, array);
});
numbers.forEach((number, index, array) => {
console.log(number, index, array);
});
fruits.forEach(function (fruit, i, arr) {
console.log(fruit, i, arr);
});
fruits.forEach((fruit, i, arr) => {
console.log(fruit, i, arr);
});
: ๋ฐฐ์ดํํ๋ก return๊ฐ์ ๋ฃ์ด์ฃผ๊ธฐ
let numbers = [1, 2, 3, 4, 5, 6];
let foreach = numbers.forEach(function (number) {
return number;
});
let map = numbers.map(function (number) {
return number;
});let map2 = numbers.map(number => {
return number;
});
console.log("foreach :", foreach);
console.log("map :", map);
console.log("map2 :", map2);
const fruits = ["์ฌ๊ณผ", "ํ์ธ์ ํ", "์๋ฐ", "ํฌ๋", "์๋ฅ์ง"];
let obj = fruits.map((item, index) => {
return {
id: index,
name: item,
}
})
console.log(obj);
let numbers = [1, 2, 3, 4, 5, 6];
let reduce = numbers.reduce(function (sum, item, index, arr) {
console.log(sum, item, index, arr);
return sum + item;
})
๋ฌธ์ :
1) 1~100๊น์ง์ ๋ฐฐ์ด์ for๋ฌธ์ ์ฌ์ฉํด์ ๋ง๋ค์ด์ฃผ๊ณ
2) 1) ๋ฐฐ์ด์ ํฉ์ reduce๋ฅผ ์ด์ฉํด์ ๊ตฌํด๋ณด์๋ฉด?
let str = "apple";
[...str].forEach(function (item, index, arr) {
console.log(item, index, arr
});
์ถ๋ ฅ๋ด์ฉ -> ["a", "p", "p", "l", "e" ]
let numbers = [1, 2, 3, 4, 5, 6];
let arr;
arr = numbers.filter((num) => num > 3);
console.log(arr);
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
console.log(result);
<์์1>
ํ์ด์์์ฑํ ํจ์
let arr1 = numbers.filter(function(item){
return item> 3;
})
ํ์ดํํจ์
let arr2 = numbers.filter((item) => item> 3);
<์์ 2>
ํ์ด์์์ฑํ ํจ์
let arr3 = words.filter(function(item) {
return item,length >= 6;
})
ํ์ดํํจ์
const result = words.filter(word => word.length > 6);
let numbers = [1, 2, 3, 4, 5, 6];
console.log(numbers.includes(3));
console.log(numbers.includes(7));
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
console.log(words.includes('elite'));
console.log(words.includes('pororo'));
๋ฌธ์ :
1) ์์ ๋ ๋ฐฐ์ด์์ ๋์ผํ ์์๋ง์ ๊ฐ์ง๋ ๋ฐฐ์ด same ๋ง๋ค๊ธฐ
2) ์์ ๋ ๋ฐฐ์ด์์ ์๋ก ๋ค๋ฅธ ์์๋ง์ ๊ฐ์ง๋ ๋ฐฐ์ด diff ๋ง๋ค๊ธฐ
const fruits = ["Apple", "Banana", "Cherry"];
const result1 = fruits.find(item => {
return /^A/.test(item);
})
const result2 = fruits.findIndex(item => {
return /^C/.test(item);
})
console.log(result1);
console.log(result2);