https://programmers.co.kr/learn/courses/30/lessons/12950
const arr1 = [[1,2],[2,3]]
const arr2 = [[3,4],[5,6]]
function solution(arr1, arr2) {
let answer = [[]];
for(let i = 0; i < arr1.length; i++){
answer[i] = [];
for(let j = 0; j < arr1[i].length; j++){
answer[i].push(arr1[i][j] + arr2[i][j])
}
}
return answer;
}
console.log(solution(arr1,arr2)); // [[4,6],[7,9]]
- 배열안의 원소가 배열이기때문에 중첩 for문을 활용한다.
const arr1 = [[1,2],[2,3]]
const arr2 = [[3,4],[5,6]]
function solution(arr1, arr2) {
return arr1.map((arr, i) => arr.map((n, j) => n + arr2[i][j]));
}
console.log(solution(arr1,arr2)); // [[4,6],[7,9]]
- arr1 의 배열을 수정하기 위해 arr1.map()을 사용한다. arr1.map(arr, i)의 요소값과 인덱스값이 들어가는데 요소값인 arr은 [[1,2],[2,3]]를 가르킨다.