유용한 10가지 배열 함수들
{
const fruits = ['apple', 'banana', 'orange'];
}
let str='';
for (let i=0; i<fruits.length; i++) {
str.push(fruits[i]);
}
fruits.join();
{
const fruits = '🍎, 🥝, 🍌, 🍒';
}
let arr = [];
for (let i = 0; i < fruits.length; i++) {
arr.push(fruits[i]);
}
fruits.split(',');
{
const array = [1, 2, 3, 4, 5];
}
array.reverse();
let arr = []
for (let i = array.length-1; i > 0; i--) {
arr.push(array[i]);
}
array.reverse();
{
const array = [1, 2, 3, 4, 5];
}
array.shift()
array.shift()
const result = array.splice(0, 2);
console.log(result);
console.log(array);
const result = array.slice(2, 5);
console.log(result);
console.log(array);
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),
];
{
for (let student of students) {
if (Student[score] === 90) return Student;
}
const result = students.find((student) => return student.score === 90);
}
{
let arr = []
for (let student of students) {
if (Student[enrolled] === true) arr.push(Student);
}
const result = students.filter((student) => return student.enrolled);
}
{
let arr = []
for (let student of students) {
arr.push(Student[score]);
}
students.map((student) => student.score)
}
{
for (let student of students) {
if (Student[score] < 50) return true;
else return false;
}
const result = students.some((student) => student.score < 50);
}
{
let arr = []
for (let student of students) {
arr.push(Student[score]);
}
const sum = arr.reduce((a, b) => a + b, 0);
const average = sum / arr.length;
const result = students.reduce((prev, curr) => prev + curr.score, 0);
console.log(result / students.length);
}
{
let str = '';
for (let student of students) {
arr.push(Student[score]);
}
const result = students
.map((student) => student.score)
.filter((score) => score >= 50)
.join();
console.log(result);
}
{
let arr = []
for (let student of students) {
arr.push(Student[score]);
}
arr.sort((a, b) => a - b);
const result = students.map((student) => student.score)
.sort((a, b) => a - b)
.join()
console.log(result);
}