[TIL] data 구조 변경하기

이나현·2021년 12월 1일
0

JavaScript

목록 보기
12/13

[1안] map으로 돌려서 내보내기
var items = [];
var rtn = items.map(item => {return {...item,dId: item.delivery.id}; })

[2안] JSON.stringify로 객체를 string으로 변경하기

  • JSON.stringify는 객체를 string으로 변경
  • JSON.parse는 string 값을 object로 변경하는 것
var obj = {id: '14', discount: null, title: '핑크', color: '핑크', child: {id: "childId"}}

//stringify를 사용
var copy = JSON.stringify(obj)
//결과:'{"id":"14","discount":null,"title":"핑크","color":"핑크","child":{"id":"childId"}}'

//parse를 사용 
var parsed = JSON.parse(copy)
//결과: {id: '14', discount: null, title: '핑크', color: '핑크', child: {…}}
  • 깊은 복사와 얕은 복사를 공부하자!! + 실습까지
profile
technology blog

0개의 댓글