arr.push()와 arr.push(...) 비교

김민호·2022년 3월 12일
0

JavaScript

목록 보기
21/21

contents.push(result)

let contents = []

result = [
  {
    name:"하하",
    age:31,
    height:"180cm"
  },
    {
    name:"호호",
    age:31,
    height:"160cm"
  }
]

a = contents.push(result) // 배열 안에 배열 자체가 한 덩어리로 들어간다

console.log(a) // 1

console.log(contents)
/* 
[
  [
    { name: '하하', age: 31, height: '180cm' },
    { name: '호호', age: 31, height: '160cm' }
  ]
]
*/

contents.push(...result)

let contents = []

result = [
  {
    name:"하하",
    age:31,
    height:"180cm"
  },
    {
    name:"호호",
    age:31,
    height:"160cm"
  }
]

a = contents.push(...result)

console.log(a) // 2

console.log(contents)
/*
[
  { name: '하하', age: 31, height: '180cm' },
  { name: '호호', age: 31, height: '160cm' }
]
*/
profile
개발자로서의 삶은 https://velog.io/@maxminos 에서 기록하고 있습니다 😀

0개의 댓글