{
const fruits = ['apple', 'banana', 'orange'];
const str = fruits.join(', ');
console.log(str)
}
apple, banana, orage
{
const fruits = 'apple, kiwi, bnanan, cherry';
const arr = fruits.split(',');
console.log(arr)
}
['apple', 'banana', 'orange']
{
const array = [1, 2, 3, 4, 5];
const reverse_array = array.reverse();
console.log(reverse_array)
}
[5, 4, 3, 2, 1]
{
const array = [1, 2, 3, 4, 5];
const slice_array = array.slice(2, 5)
console.log(slice_array)
}
[3, 4, 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('F', 18, true, 88), ]
{
// const arr = [];
// students.forEach((student) => {
// if (student.score === 90){
// arr.push(student)
// }
// })
// console.log(arr)
const result = students.find(function(student, index){
return student.score === 90;
})
const result2 = students.find(student => student.score === 90)
console.log(result2);
}
find
{
// const result = [];
// students.forEach(student => {
// if(student.enrolled)
// result.push(student)
// })
// console.log(result)
const result = students.filter(student => student.enrolled);
console.log(result)
}
filter
result should be: [45, 80, 90, 66, 88]
{
const result = students.map(student => student.score);
console.log(result)
}
map
{
const result = students.some(student => student.score < 50);
console.log(result)
}
or
같은 느낌and
와 같은 역할을 하는 every
메소드도 있음{
const result = students.reduce((prev, curr) => prev + curr.score, 0)
console.log(result / students.length)
// reduceRigth은 거꾸로 간다.
}
reduce
는 좀 어려운? 문법인데, prev, curr개념을 잘 알아야 할 것 같다.result should be: '45, 80, 90, 66, 88'
{
const result = students.map(student => student.score).join(', ');
console.log(result)
}
result should be: '45, 66, 80, 88, 90'
{
const result = students.map(student => student.score)
.sort((a, b) => a-b)
.join(', ');
console.log(result)
const result2 = students
.sort((a, b) => b.score - a.score)
console.log(result2)
}
result2
는 객체도 마찬가지로 되나 싶어서 테스트 잘됨!