react 배열 수정하기(map과 spread응용)
프론트엔드/리액트-데어프로그래밍 강좌정리2021. 7. 21. 14:33
배열 변경하기
const users=[
{id:1, name:"구태모", phone="2222"},
{id:2, name:"이대엽", phone="3333"},
{id:3, name:"오승훈", phone="4444"},
];
const updateUserDto= {
id:2, name:"홍길동"
};
const users 를 안바꾸고 변경해서 새로운 객체로 업데이트하려면 ?
const newUser = users.map(user=>user.id===updateUserDto.id?{...u, ...updateUserDto} : user); // const newUser={...users};
console.log("newUser", newUsers);
연습하기>
const a6 = {id:1, name:"홍길동"};
const b5={...a6, name:"임꺽정"}; // 제일 뒤에로 덮어씌운다.
console.log(b6); //{id:1, name:"임꺽정"}
'프론트엔드 > 리액트-데어프로그래밍 강좌정리' 카테고리의 다른 글
react useEffect (0) | 2021.07.22 |
---|---|
react useState (0) | 2021.07.21 |
react 배열(map, filter, slice, concat, spread연산자) (0) | 2021.07.21 |
react 기본문법 (0) | 2021.07.21 |
react 실행흐름 이해하기 (0) | 2021.07.21 |
댓글()