Arrays APIs
2021. 7. 7. 21:33ใBig Dreamer_Developer/JS
// Q1. make a string out of an array
{
const fruits = ['apple', 'banana', 'orange'];
const result = fruits.join(',');
console.log(result);
}
// Q2. make an array out of a string
{
const fruits = '๐, ๐ฅ, ๐, ๐';
const result = fruits.split(',');
console.log(result);
// const result = fruits.split(',', 2);
// console.log(result);
}
// Q3. make this array look like this: [5, 4, 3, 2, 1]
{
const array = [1, 2, 3, 4, 5];
const result = array.reverse();
console.log(result);
console.log(array);
}
// Q4. make new array without the first two elements
{
// slice == ๋ฐฐ์ด์์ ์ํ๋ ๋ถ๋ถ๋ง returnํด์ ๋ฐ์์ค๋ ์์ด
const array = [1, 2, 3, 4, 5];
const result = array.slice(2, 5);
console.log(result); // [3, 4, 5]
console.log(array); // [1, 2, 3, 4, 5]
// splice == ๋ฐฐ์ด ์์ฒด๋ฅผ ์์ ํด์ฃผ๋ ์์ด
// const array = [1, 2, 3, 4, 5];
// const result = array.splice(0, 2);
// console.log(result); // [1, 2]
// console.log(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('E', 18, true, 88),
];
// Q5. find a student with the score 90
{
const result = students.find((student) => student.score === 90);
console.log(result);
}
// Q6. make an array of enrolled students
{
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 result = students.map((student) => student.score);
console.log(result);
}
// Q8. check if there is a student with the score lower than 50
{
console.clear();
const result = students.some((student) => student.score < 50);
console.log(result); // true
const result2 = students.every((student) => student.score < 50);
console.log(result2); // false
const result3 = !students.every((student) => student.score >= 50);
console.log(result3); // true
console.log(!true) // false
}
console.clear();
// Q9. compute students' average score
{
const result = students.reduce((prev, curr) => prev.score + curr.score);
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)
.filter((score) => score >= 50)
.join();
console.log(result);
}
// 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) => b - a)
.join();
console.log(result);
}
// start with strings, numbers and booleans
// let age = 100;
// let age2 = age;
// console.log(age, age2);
// age = 200;
// console.log(age, age2);
// let name = 'Wes';
// let name2 = name;
// console.log(name, name2);
// name = 'wesley';
// console.log(name, name2);
// Let's say we have an array
const players = ['Wes', 'Sarah', 'Ryan', 'Poppy'];
// and we want to make a copy of it.
const team = players;
console.log(players, team);
// You might think we can just do something like this:
// not the same...with numbers, string, boolean
team[3] = 'Lux';
// ๋ฐฐ์ด์ ์
๋ฐ์ดํธ ํ๋ฉด ํญ์ reference ์์ฒด์ ๋ณํ๊ฐ ์๊ธด๋ค.
// ์ค, ๊ทธ๋ฅ ๋ณต์ฌ๋ณธ์ ๋ง๋ค๊ณ ์ถ์ต๋๋ค. How can do that?
// reference์ ๋ฐฐ์ด์ ์ฌ๋ณธ์ ๋ง๋ค ์ ์๋ ๋ช ๊ฐ์ง ๋ฐฉ๋ฒ์ด ์์ต๋๋ค.
// however what happens when we update that array?
// now here is the problem!
// oh no - we have edited the original array too!
// Why? It's because that is an array reference, not an array copy. They both point to the same array!
// So, how do we fix this? We take a copy instead!
// const team2 = players.slice();
// console.log(team2);
// const team3 = [].concat(players);
// console.log(team3);
// team2[3] = 'Hyobum';
// team3[3] = 'Hyobum';
// console.log(team2);
// console.log(team3);
// console.log(players);
// players[0] = 'David';
// console.log(team2);
// console.log(team3);
// console.log(players);
// ์ผ๋ฐ์ ์ผ๋ก ์ฌ๋ผ์ด์ค๊ฐ ํ๋ ์ผ์ 2๊ฐ์ 3๊ฐ๋ฅผ ์์ฒญํ๋ ๊ฒ์
๋๋ค.
// ๊ทธ๋ฌ๋ฉด ์๋ ๋ฐฐ์ด์ ๊ทธ๋๋ก ์ ์ง๋์ง๋ง
// ์๋ฌด๊ฒ๋ ์ ๋ฌํ์ง ์์ผ๋ฉด ์ฌ๋ผ์ด์ค๋ ์ฌ๋ณธ์ ์ญํ ์ ํ ์ ์๋ค.
// players์ item์ ๋ณํ๋ฅผ ์ฃผ๋๋ผ๋ team2์๋ ์๋ฌด๋ฐ ๋ณํ๊ฐ ์๋ค.
// ์ธ๋ฒ์งธ team์ ๋ฐฐ์ด์ ๊ฐ์ ธ ์์ ๊ธฐ์กด ๋ฐฐ์ด์ ์ฐ๊ฒฐํฉ๋๋ค.
// ๊ทธ๋ฌ๋ฉด ํด๋น players ๋ฐฐ์ด์ ๋ชจ๋ ํญ๋ชฉ์ ๋น ๋ฐฐ์ด์ team2์ ๋๊ฐ์ ๋ฐฉ์์ผ๋ก ์๋ํฉ๋๋ค.
// one way
// or create a new array and concat the old one in
// or use the new ES6 Spread
// ES6์ Spread operator๋ฅผ ์ด์ฉํ๋ ๋ฐฉ๋ฒ
const team4 = [...players];
team4[3] = 'heee hawww';
console.log(team4);
// ์คํ๋ ๋๊ฐ ํ๋ ์ผ์ iterable์์ ํญ๋ชฉ์ ์ ๊ฑฐํ๊ณ ์ด ๊ฒฝ์ฐ ํฌํจ์ ๋ฃ๋๋ค.
// team4์ item์ ๋ณ๊ฒฝํด๋ players์ item๋ค์ ๋ณํ์ง ์๊ณ ๊ทธ๋๋ก ์๋ค.
const team5 = Array.from(players)
// Array.from() ์ ์ด์ฉํ๋ ๋ฐฉ๋ฒ๋ ์๋ค.
team5[3] = 'hahahahahahah';
// players๋ ์๋์ง ์์ ๋ฐ๋ฉด ํ 5์ ์
๋ฐ์ดํธ๋์์ต๋๋ค.
// now when we update it, the original one isn't changed
// The same thing goes for objects, let's say we have a person object
// with Objects
const person = {
name: 'Wes Bos',
age: 80
};
// ์ฌ๊ธฐ์ ์ฌ๋ ๊ฐ์ฒด๊ฐ ์๊ณ
// and think we make a copy:
// const captain = person;
// captain.number = 99;
// ์ฌ๋ณธ์ด ์๊ธฐ๋ ๊ฒ์ด ์๋ reference๋ฅผ ์ฐธ์กฐํจ => ์๋ณธ์ด ๋ณ๊ฒฝ๋๋ฒ๋ฆผ.
// ๋ฐ๋ผ์ ๋์ ์ Object.assign ์ ์ฌ์ฉํ์ฌ ์ฌ๋ณธ์ ๋ง๋ค์ด์ผ ํฉ๋๋ค.
// how do we take a copy instead?
const cap2 = Object.assign({}, person, { number: 99, age: 12 });
console.log(cap2);
// Object.assign์ ์ ๊ณ ๊ทธ ํ ์ ๋ฌํ๋ ๋น ๊ฐ์ฒด๋ก ์์ํฉ๋๋ค. => ์ฒซ ๋ฒ์งธ ์ธ์
// ์ด ๋น ๊ฐ์ฒด ์์๋(,) person ์ฆ ๋ณต์ฌํ๋ ค๋ ๊ฐ์ฒด๋ฅผ ์ ์ต๋๋ค. => ๋๋ฒ์งธ ์ธ์
// ๊ทธ ์์๋(,) ๋น์ ์ด ์ํ๋ ์๋ก์ด ์์ฑ์ ์ ์ต๋๋ค. => ์ธ ๋ฒ์งธ ์ธ์
// cap2 ๋ผ๋ person์ reference๋ฅผ ๋ณ๊ฒฝํ์ง ์๋ object๊ฐ ๋ง๋ค์ด์ก์ต๋๋ค.
// cap2 ์ item๋ค์ ์์ ํ๊ฑฐ๋ ์ถ๊ฐํ์ฌ๋ person ์๋ณธ์๋ ์๋ฌด๋ฐ ๋ณํ๊ฐ ์์ต๋๋ค.
// We will hopefully soon see the object ...spread
const cap3 = {...person};
cap3.name = 'Hyobum';
console.log(cap3);
// ์์ ๋ณต์ฌ? ์ด๊ฒ๋ ์๋ณธ์ ๊ฑด๋ค์ด์ง ์๋ ์ฌ๋ณธ์ด ๋ง๋ค์ด์ง๋ค.
// ํ์ง๋ง ์ฃผ์ํด์ผ ํ ์ ์ด ์์. ๊ทธ ๋ด์ฉ์ ๋ฐ์...
// Things to note - this is only 1 level deep - both for Arrays and Objects. lodash has a cloneDeep method, but you should think twice before using it.
const wes = {
name: 'Wes',
age: 100,
social: {
twitter: '@wesbos',
facebook: 'wesbos.developer'
}
};
console.clear()
// console.log(wes);
const dev = Object.assign({}, wes);
// name์ ์ฌ๋ณธ์ ํ์์ผ๋ก ๋๋ ๋ฐ ๋ฐํด...
// social์ ์๋ณธ์ ์ฐธ์กฐํ๊ฒ ๋๋ค.
dev.social.twitter = '@coolman';
console.log(dev.social); // {twitter: "@coolman", facebook: "wesbos.developer"}
console.log(wes.social); // {twitter: "@coolman", facebook: "wesbos.developer"}
// ์๋ณธ์ reference๊ฐ ๊ทธ๋๋ก ์์ ๋๋ฒ๋ฆฌ๋ ๋ฌธ์ ๊ฐ ์๋ค.
// ๋จ์ง ํ ์์ค ๊น์ ๊ณณ์์ ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ ๋ ์ฌ์ฉํ ์ ์์ง๋ง
// ํ ์์ค ๋ ๊น์ ๊ณณ์ผ๋ก ๊ฐ๊ฒ ๋๋ฉด ์๋ณธ reference๋ฅผ ์นจํดํ๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํฉ๋๋ค.
// Deep Cloning์ ์๊ฒ ๋๋ฉด ์ด๋ฌํ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์๋ค. -Team Jupeter
// wesbos์ ์ถ์ฒํ์ง ์์ง๋ง, ์ด๋ฌํ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ =>
const dev2 = JSON.parse(JSON.stringify(wes));
// ์ ๊ฐ์ ๋ฐฉ์์ผ๋ก ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์์ต๋๋ค.
// ์ด์ : json stringify๋ฅผ ์ทจํ๊ธฐ ๋๋ฌธ.
// ๊ฐ์ฒด๋ฅผ ์ ๋ฌํ๋ฉด ๋ ์ด์ ๋ฌธ์์ด์ด ์๋ ๋ฌธ์์ด๋ก ๋ฐํ๋ฉ๋๋ค.
// ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ฉด ์ฆ์ json.parse๋ ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ์ง ์๊ณ
// ์ ์ฒด ์ฌ๋ณธ์ ์ป๊ณ ์ฐธ์กฐ์ ๋ํด ์ ํ ๋ณ๊ฒฝ์ด ์๋๊ฒ ํ๋ ๊ฒ์
๋๋ค.
// ์ด๋ ๋ถ์ํ ์ฌ๋์ ๋ฅ ํด๋ก๋.........
// ์ด ์ธ์ ๋ฐฉ๋ฒ ํ ๋๊ฐ์ง๋ฅผ ์ข ๋ ๊ณต๋ถํ๊ธฐ๋ฅผ ๋ฐ๋๋๋ค.
35. Object assign
let student1 = {
name: "Superman",
study: "Javascript"
}
let student2 = {};
Object.assign(student2, student1);
console.log(student1.name); // Superman
console.log(student1.name); // Superman
let student1 = {
name: "Superman",
study: {
field: "JavaScript",
year: 3
}
}
let student2 = {
name: "Ironman",
study: {
field: ""
},
};
// console.log(student2.name);
Object.assign(student2, student1);
// console.log(student1.name); // Superman
// console.log(student2.name); // Superman
student2.name = "Birdman";
student2.study.field = "Haskell";
// console.log(student1.name); // Superman
// console.log(student2.name); // Birdman
console.log(student2.study); // { field: 'Haskell', year: 3 }
console.log(student1.study); // { field: 'Haskell', year: 3 }
console.log(student2.study.field); // Hakell
console.log(student2.study.year); // 3
let student1 = {
name: "Superman",
study: "Javascript"
}
let student2 = {
name: "Ironman",
study: ""
};
console.log(student2.name); // Ironman
Object.assign(student2, student1);
student2.name = "Birdman";
console.log(student1.name); // Superman
console.log(student2.name); // Birdman
console.log(student2.study); // Javascript
36. Object Deep Cloning
let student1 = {
name: "Superman",
study: {
field: "Javascript",
year: 3
}
};
let student2 = {
name: "Ironman",
study: {
field: ""
}
};
// console.log(student2.name);
Object.assign(student2, student1);
// console.log(student2.name);
student2.name = "Birdman";
student2.study.field = "Haskell";
// console.log(student1.name);
// console.log(student2.name);
console.log(student2.study); // {field: "Haskell", year: 3}
console.log(student1.study); // {field: "Haskell", year: 3}
console.log(student1.name); // Superman
// Deep Cloning -> Object ์์ ์๋ Object๋ ๋ณต์ฌํ ๋ ์์ ํ ์๋ก์ด Object๊ฐ ๋ง๋ค์ด์ง๊ฒ ํ๋..
- ์ถ์ฒ: TEAM JUPETER
'Big Dreamer_Developer > JS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ES6, ES11 Syntax Summary (0) | 2021.07.06 |
---|---|
JavaScript Class & Callback function (0) | 2021.07.06 |
JavaScript async & await, Promise API (0) | 2021.07.06 |
JavaScript Promise (0) | 2021.07.06 |
Call Back Function (0) | 2021.07.06 |