Spread syntax

  • if you want to add array inside middle of array
    1
    2
    const spread = [123];
    const arr = [0, ...spread, 4]; // [0, 1, 2, 3, 4])
    cs
  • if it is empty array, it will return nothing
    1
    2
    const spread = [];
    const arr = [0, ...spread, 4]; // [0, 4]
    cs
  • can merge arrays same as concat method
    1
    2
    3
    4
    const arr1 =[123];
    const arr2 = [345];
    const concatenated = [...arr1, ...arr2]; // [0, 1, 2, 3, 4, 5]
    arr1.concat(arr2); // [0, 1, 2, 3, 4, 5]
    cs
  • can merge objects as well
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    const paige = {
        firstName: 'Paige',
        LastName: 'Kim',
    };
    const me = {
        hobby: 'piltaes',
        todos: ['coplit''koans'],
    };
     
    const merged = {...paige, ...me};
    /* const merged = {
        firstName: 'Paige',
        LastName: 'Kim',
        hobby: 'piltaes',
        todos: ['coplit', 'koans'],
    };
    */
    cs
  • if spread syntax goes inside parameter, it become 'rest parameter(rest syntax)'
    • useful when numbers of parameters were not set
    • returns arguments with 'ARRAY'
      1
      2
      3
      4
      function pintMaxNums(...args) { 
          console.log(args); //[10, 30, 40]
      }
      pintMaxNums(103040); 
      cs

Object

  • === 
    • To compare 'REFERENCE' only in object!!
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      const person = {
            son: {
              age: 9,
            },
          };
          
      const boy = person.son;
      boy.age = 20;
      expect(person.son.age).toBe(20);
      expect(person.son === boy).toBe(true);
      expect(person.son === { age: 9 }).toBe(false);
      expect(person.son === { age: 20 }).toBe(false);  
      cs

 

'TIL' 카테고리의 다른 글

TIL 2020.11.11  (0) 2020.11.14
TIL 2020.11.09  (0) 2020.11.09
TIL 2020.11.07  (0) 2020.11.07
TIL 2020.11.06  (0) 2020.11.06
TIL 2020.11.05  (0) 2020.11.05

+ Recent posts