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