
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
console.log(result);
결과
["exuberant", "destruction", "present"]
const arr = [
{ num: 1, color: "red" },
{ num: 2, color: "black" },
{ num: 3, color: "blue" },
{ num: 4, color: "green" },
{ num: 5, color: "yellow" }
];
console.log(arr.slice(0, 4));
결과
[Object, Object, Object, Object]
0 : Object
num: 1
color: "red"
...
이런식으로 나옴
(생략)
const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2);
console.log(array3);
console.log(array1.concat(array2));
결과
["a", "b", "c", "d", "e", "f"]
숫자를 sort할 때는 비교함수를 만들어서 넣어줘야한다.
let numbers = [0, 1, 3, 2, 10, 30, 20];
const compare = (a, b) => {
if (a > b) {
// 크다
return 1;
}
if (a < b) {
// 작다
return -1;
}
// 같다
return 0;
};
numbers.sort(compare);
console.log(numbers);
결과
[0, 1, 2, 3, 10, 20, 30]
-> 내림차순으로 정렬하려면 compare함수의 return 값을 반대로 바꿔주면 된다.
const arr = ["나는", "야", "멋쟁이", "토마토입니다"];
console.log(arr.join());
console.log(arr.join(" "));
console.log(arr.join("ㅁ"));
결과
나는,야,멋쟁이,토마토입니다
나는 야 멋쟁이 토마토입니다
나는ㅁ야ㅁ멋쟁이ㅁ토마토입니다