개발공부/Javascript
9. 유용한 10가지 배열 함수들. Array APIs 총정리
MyAlice
2021. 4. 27. 14:30
728x90
** 이 글은 유튜브 강의을 보고 공부하면서 스스로 콘솔로 작성하고, 따라치면서 Javascript 기초 공부를 하는 공간입니다.
www.youtube.com/watch?v=3CUjtKJ7PJg&list=PLv2d7VI9OotTVOL4QmPfvJWPJvkmv6h-2&index=9
// Q1. make a string out of an array (틀림ㅜㅜㅜㅜ)
{
const fruits = ['apple', 'banana', 'orange'];
const result = fruits.join('|');
const result2 = fruits.join();
console.log(result); //"apple|banana|orange"
console.log(result2); //"apple,banana,orange"
}
// Q2. make an array out of a string(틀림ㅜㅜㅜㅜ)
{
const fruits = '🍎, 🥝, 🍌, 🍒';
const result = fruits.split(',');
console.log(result); //["🍎"," 🥝"," 🍌"," 🍒"]
}
// Q3. make this array look like this: [5, 4, 3, 2, 1]
{
const array = [1, 2, 3, 4, 5];
const newArray = [];
for(let a = array.length -1 ; a >= 0; a--){
newArray.push(array[a]);
}
console.log(newArray, "Q3");
// 해결방법
const result = array.reverse(); //array 배열자체도 reverse 하게 되어진다!
console.log(result); // [5, 4, 3, 2, 1]
console.log(array); // [5, 4, 3, 2, 1]
}
// Q4. make new array without the first two elements
{
const array = [1, 2, 3, 4, 5];
array.shift();
array.shift();
// console.log(array, "Q4");
// 해결방법
const result = array.slice(2,5);
console.log(result); //[3,4,5]
console.log(array); //[1, 2, 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('E', 18, true, 88),
];
// Q5. find a student with the score 90
{
for( value of students ){
if(value['score'] == '90'){
// console.log(value['name'], "Q5");
}
}
// 해결방법
const result = students.find((student) => student.score == 90);
console.log(result);
}
// Q6. make an array of enrolled students
{
const arry6 =[];
for(value of students){
if(value['enrolled']){
arry6.push(value);
}
}
// console.log(arry6, "Q6");
// 해결방법
const result = students.filter((student) => student.enrolled);
console.log(result);
}
// Q7. make an array containing only the students' scores
// result should be: [45, 80, 90, 66, 88]
{
const arr7 = [];
for(let i = 0; i < students.length ; i++ ){
arr7.push(students[i]['score']);
}
// console.log(arr7, "Q7");
// 해결방법
// 다른 방식의 데이터로 바꾸는것 !
const result = students.map((student) => student.score);
console.log(result);
}
// Q8. check if there is a student with the score lower than 50
{
const arr8 = [];
for(let i = 0; i < students.length ; i++ ){
if(students[i]['score'] <= 50 ){
// console.log(students[i]['name'], "Q8");
}
}
// 해결방법
const result = students.some((student) => student.score < 50 ); // 있는지 없는지 확인하는것
console.log(result); // true
const result2 = students.every((student) => student.score < 50 ); // 모든 배열이 충족되었는지 확인하는것
console.log(result2); // false
}
// Q9. compute students' average score
{
let average = 0;
for(let i = 0; i < students.length ; i++ ){
average += students[i]['score'];
}
//console.log(average / students.length , "Q9");
// 해결방법
const result = students.reduce((prev, curr) => {
console.log('---------');
console.log(prev);
console.log(curr);
return prev + curr.score;
}, 0);
console.log(result / students.length); //73.8
}
// Q10. make a string containing all the scores
// result should be: '45, 80, 90, 66, 88'
{
const result = students.map((student) => student.score).join();
console.log(result); //'45, 80, 90, 66, 88'
const result1 = students
.map((student) => student.score)
.filter((student) => student >= 50)
.join();
console.log(result1); // 80, 90, 66, 88'
}
// Bonus! do Q10 sorted in ascending order
// result should be: '45, 66, 80, 88, 90'
{
const result = students
.map((student) => student.score)
.sort((a,b) => a - b )
.join()
;
console.log(result); //'45, 66, 80, 88, 90'
}
728x90