๐ญ push
๋ฐฐ์ด ๋ท๋ถ๋ถ์ ๊ฐ์ ์ฝ์ ํ๋ค.
var arr = ["a", "b", "c", "d"];
arr.push("e");
console.log(arr); // ["a", "b", "c", "d", "e"]
๐ญ pop
๋ฐฐ์ด ๋ท๋ถ๋ถ์ ๊ฐ์ ์ญ์ ํ๋ค.
var arr = ["a", "b", "c", "d"];
arr.pop();
console.log(arr); // ["a", "b", "c"]
๐ญ shift
๋ฐฐ์ด ์๋ถ๋ถ์ ๊ฐ์ ์ญ์ ํ๋ค.
var arr = ["a", "b", "c", "d"];
arr.shift();
console.log(arr); // ["b", "c", "d"]
๐ญ unshift
๋ฐฐ์ด ์๋ถ๋ถ์ ๊ฐ์ ์ฝ์ ํ๋ค.
var arr = ["a", "b", "c", "d"];
arr.unshift("z");
console.log(arr); // ["z", "a", "b", "c", "d"]
๐ญ splice
๋ฐฐ์ด์ ํน์ ์์น์ ์์๋ฅผ ์ถ๊ฐํ๊ฑฐ๋ ์ญ์ ํ๋ค.
var arr = ["a", "b", "c", "d"];
arr.splice(3, 1); // 3๋ฒ์งธ ์ธ๋ฑ์ค์์๋ถํฐ 1๊ฐ ์ ๊ฑฐ
console.log(arr); // ["a", "b", "c"]
var arr = ["a", "b", "c", "d"];
arr.splice( 3, 1, "e", "f"); // 3๋ฒ์งธ ์ธ๋ฑ์ค์์ 1๊ฐ ์ ๊ฑฐ ํ "e"์ "f" ์ถ๊ฐ
console.log(arr); // ["a", "b", "c", "e", "f"]
๐ญ slice
๋ฐฐ์ด์ startIndex๋ถํฐ endIndex๊น์ง(endIndex๋ ํฌํจx)์ ๋ํ ์์ ๋ณต์ฌ๋ฅผ ์๋ก์ด ๋ฐฐ์ด ๊ฐ์ฒด๋ก ๋ฐํํ๋ค.
๐ฅ ์์ ๋ณต์ฌ(Shallow Copy)๋?
๊ฐ์ฒด๋ฅผ ๋ณต์ฌํ ๋ ์๋ ๊ฐ๊ณผ ๋ณต์ฌ๋ ๊ฐ์ด ๊ฐ์ ์ฐธ์กฐ๋ฅผ ๊ฐ๋ฆฌํค๊ณ ์๋ ๊ฒ์ ๋งํ๋ค.
var arr = ["a", "b", "c", "d"];
var newArr = arr.slice();
console.log(newArr); // ["a", "b", "c", "d"]
๐ญ concat
์ฌ๋ฌ ๊ฐ์ ๋ฐฐ์ด์ ํฉ์น๊ณ ๋ณํฉ๋ ๋ฐฐ์ด์ ์ฌ๋ณธ์ ๋ฐํํ๋ค.
var arr1 = ["a", "b", "c", "d"];
var arr2 = ["e", "f"];
var arr3 = arr1.concat(arr2);
console.log(arr3); // ["a", "b", "c", "d", "e", "f"]
๐ญ some
ํจ์์ ๊ฒฐ๊ณผ๊ฐ ์ฐธ(true)์ผ ๋๊น์ง ๋ฐฐ์ด์ ๊ฐ ์์๋ฅผ ๋ฐ๋ณตํ๋ค.
var arr = ["a", "b", "c", "d"];
var isEven = function(value) { // value๊ฐ "a"์ด๋ฉด true๋ฅผ ๋ฐํํ๋ค.
return value == "a";
};
console.log(arr.some(isEven)); // true
๐ญ every
๋ฐฐ์ด์ ๊ฐ element์ ๋ํด์ ํ ์คํธ ํจ์์ ๋ฐํ ๊ฐ์ด ๋ชจ๋ true์ธ์ง ํ์ธํ๋ค. ์ฆ, ๋ชจ๋ case๊ฐ ์ฐธ์ผ๋ true๋ฅผ ๋ฐํํ๋ค.
var arr = [{name: '์งฑ๊ตฌ', age: 10}, {name: '์งฑ์', age: 2}, {name: '๋งน๊ตฌ', age: 15}]
console.log(arr.every((item)=> item.age>10)); // false (๋ฐ๋์ด ํ๋ฝ!)
console.log(arr.every((item)=> item.age>1)); // true
๐ญ forEach
๋ฐฐ์ด์ ๊ฐ ์์์ ๋ํด callback์ ์คํํ๋ค.
(but, ์ค๊ฐ์ break๋ฌธ ์ฌ์ฉ ๋ชปํจ)
var arr = ["a", "b", "c", "d"];
arr.forEach(function(value) {
console.log(value); // a, b, c, d
});
๐ญ map
forEach์ ๋ง์ฐฌ๊ฐ์ง๋ก Array์ ๊ฐ ์์๋ฅผ ์ํํ๋ฉฐ callback ํจ์๋ฅผ ์คํํ ํ, callback์์ return ๋๋ ๊ฐ์ ๋ฐฐ์ด๋ก ๋ง๋ค์ด๋ธ๋ค.
var arr1 = ["a", "b", "c", "d"];
var isEven = function(value) {
return value == "a";
};
var arr2 = arr1.map(isEven);
console.log(arr2); // [true, false, false, false]
๐ญ filter
callback ํจ์์ ๊ฒฐ๊ณผ ๊ฐ์ true๋ก ๋ง๋๋ ์์๋ค๋ก๋ง ๊ตฌ์ฑ๋ ๋ณ๋์ ๋ฐฐ์ด์ ๋ฐํํ๋ค.
var arr1 = ["a", "b", "c", "d"];
var isEven = function(value) {
return value == "a";
};
var arr2 = arr1.filter(isEven);
console.log(arr2); // ["a"]
๐ญ reduce
๋ฐฐ์ด์ ๊ฐ ์์์ ๋ํด callback์ ์คํํ๋ฉฐ ๋จ 1๊ฐ์ ์ถ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ๋ง๋ ๋ค.
๐ญ reverse
๋ฐฐ์ด์ ์์ ์์๋ฅผ ๊ฑฐ๊พธ๋ก ๋ฐ๊พผ๋ค.
var arr = ["a", "b", "c", "d"];
arr.reverse();
console.log(arr); // ["d", "c", "b", "a"]
๐ญ sort
๋ฐฐ์ด์ ์์๋ฅผ ์ํ๋ฒณ์์ผ๋ก, ๋๋ ์ง์ ๋ ํจ์์ ๋ฐ๋ฅธ ์์๋ก ์ ๋ ฌํ๋ค.
(๋ชจ๋ ์์๋ฅผ ๋ฌธ์์ด๋ก ์ทจ๊ธํด ์ฌ์ ์์๋๋ก ์ ๋ ฌํ๋ค)
var arr = ["b", "c", "a", "x", "f", "e", "y", "o"];
arr.sort();
console.log(arr); // ["a", "b", "c", "e", "f", "o", "x", "y"]
๐ญ toString
๋ฐฐ์ด์ ๋ฌธ์์ด๋ก ๋ฐ๊พธ์ด ๋ฐํํ๋ค.
var arr = [1, 2, 3, 4];
console.log(arr.toString()); // 1, 2, 3, 4
๐ญ valueOf
๋ฌธ์์ด์ ๋ํ ์์ ๋ฐ์ดํฐํ(primitive value)์ ๋ฐํํ๋ค.
var arr = [1, 2, 3, 4];
console.log(arr.valueOf()); // [1, 2, 3, 4]
๐ญ join
๋ฐฐ์ด ์์ ์ ๋ถ๋ฅผ ํ๋์ ๋ฌธ์์ด๋ก ํฉ์น๋ค.
var arr = [1, 2, 3, 4];
console.log(arr.join()); // 1, 2, 3, 4
console.log(arr.join('-')); // 1-2-3-4
'JavaScript > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JavaScript] var, let, const ์ฐจ์ด (0) | 2021.11.20 |
---|---|
์ ์ฌ ๋ฐฐ์ด ๊ฐ์ฒด(Array-Like Object) (0) | 2021.11.18 |
์์ ๋ณต์ฌ, ๊น์ ๋ณต์ฌ (0) | 2021.11.17 |
immutable.js, baobab.js (0) | 2021.11.17 |
arrow function์์ { }(์ค๊ดํธ)๊ฐ ์๊ณ ์๊ณ ์ ์ฐจ์ด (0) | 2021.11.16 |
๋๊ธ