const fruits = ["apple", "banana", "kiwi"]
console.log(fruits.join()); /// 'apple,banana,kiwi'
console.log(fruits.join('')); ///'applebananakiwi'
console.log(fruits.join('&')); /// 'apple&banana&kiwi'
const fruits = '가나다라마바사';
console.log(fruits.split('')) //// [ '가', '나', '다', '라', '마', '바', '사' ]
console.log(fruits.split()) /// [ '가나다라마바사' ]
console.log(fruits[5]) /// '바'
{
const array = [1, 2, 3, 4, 5];
console.log(array.reverse()) ///[ 5, 4, 3, 2, 1 ]
}
여기서 splice와 차이점?
splice는 기존 배열을 수정한다!!!!
/// slice()
const array = [1, 2, 3, 4, 5];
const arr = array.slice(1,4)
console.log(arr) ///[ 2, 3, 4 ]
console.log(array) /// [ 1, 2, 3, 4, 5 ] => 기존배열은 수정되지 않는다.
/// splice()
const array = [1, 2, 3, 4, 5];
const arr = array.splice(1,3)
console.log(arr) ///[ 2, 3, 4 ] /// 1번째 index인 2부터 3개를 새로운 배열로 반환했다
console.log(array) ///[ 1, 5 ] /// 그리고 기존배열이 수정되었다.
class Student {
constructor(name, age, enrolled, score) {
this.name = name;
this.age = age;
this.enrolled = enrolled;
this.score = score;
}
}
const students = [
new Student('A', 29, true, 45),
new Student('B', 28, false, 80),
new Student('C', 30, true, 90),
new Student('D', 40, false, 66),
new Student('E', 18, true, 88),
];
첫번째로 찾은 요소가 true일때 true의 값을 return한다. 없다면 undefined를 반환한다.
{
const found = students.find(ele=> ele.score < 90 );
console.log(found); /// student A의 정보가 나온다.
}
{
const result = students.filter((student) => student.enrolled)
console.log(result)
} //// 학생 A, C, E의 정보가 배열형태로 나온다
{
const mapping = students.map(function(students){
return students.score
})
console.log(mapping)
} /////[ 45, 80, 90, 66, 88 ]
{
const result = students.some((student) => student.score < 50);
console.log(result) ///true
}
그럼 배열 전부가 조건에 만족하는지 체크할 수 있는 메서드는? => every()
{
const result = (prev,curr) =>
(prev + curr.score) / students.length
console.log(students.reduce(result,0))///73.80000000000001
}