1. js 예제 1주차 13번 문제 해결 과정
2. js 예제 2주차 에러가 있던 문제 해결 과정
3. 예제 3주차 에러가 있던 문제 해결 과정
3. 예제 4주차 에러가 있던 문제 해결 과정
초기 코드
const db = [
{
"id": 7,
"name": "Jay",
"age": 33,
"phone": "010-1212-5678",
"email": "qwe@gmail.com"
},
{
"id": 10,
"name": "James",
"age": 30,
"phone": "010-1234-5678",
"email": "abc@gmail.com"
}
]
function handleEdit(id, editingObj) {
let element = db.find(el => el.id === id);
[...element].replace(element, editingObj);
}
handleEdit(10, { name: "Paul", age: 35 });
console.log(db);
해결 과정
(1) Object.assign() 을 활용해 찾은 element를 editingObj로 바꾸는 코드를 입력
Object.assign(element, editingObj);
(2) 실행했더니 바뀌지 않은 db 그대로 출력됨 -> Object.assign 을 const로 선언 후 console로 찍어봄
(3) 바뀌지 않은 값이 출력 됨
(4) element를 console로 찍어봄 -> 결과 : 1
(5) .findIndex -> .find
(6) 해결
초기 코드
const idx = db.findIndex(el.id === id);
db[idx] = {...db[idx], ...editingObj};
(1) 에러: findIndex 안에는 함수가 들어가야 된다
수정코드
const idx = db.findIndex(el => el.id === id);
(3) 결과값에 배열에 element만 뜸
(4) spread 앞에 띄어쓰기 추가
db[idx] = { ...db[idx], ...editingObj };
(5) 해결
나의 풀이
function shallowCopy(obj) {
return Object.assign(obj, shallowCopied)
}
나의 초기 풀이
function mapArray(arr) {
return arr.map((el, idx) => {[idx]: el});
}
함수에서 객체를 ()로 감싸주지 않으니까 오류가 났다.
수정 코드
return arr.map((el, idx) => ({[idx]: el}));