얼음꽃의 일지

9.26~10.02 WIL 본문

WIL

9.26~10.02 WIL

얼음꽃 2022. 9. 26. 21:52
728x90

Map 매서드

▶ Map 작동 방식

- map은 배열 내의 모든 요소 가각에 대하여 주어진 함수를 호풀한 결과를 모아 새로운 배열을 반환

 

let arr = [1,2]

// 배열자체를 바로 들고옴 -> [1,2]
console.log(arr)

// arr1안에 들어있는 값을 순차적으로 꺼내옴  -> 1 줄바꿈 2
let m = arr.map(x => console.log(x))
// let n = arr1.map(x => console.log(x*2))

let arr2 = [[1,2],[3,4]]

// 풀어서 설명하면 x의 index 값을 [1,2] [3,4] 를 가져와서 그걸 map을 또 시키면 각 원소의 값을 가져올 수 있음
// 여기서 i는 index를 의미 , arr3안에 들어있는 값을 순차적으로 꺼내몽 -> 1 줄바꿈 2 줄바꿈 3 줄바꿈 4
let t = arr2.map((x,i) => x.map(x => console.log(x)))

// arr2 배열 각 원소에 2를 곱하고 그 배열 자체를 들고옴 -> [ [ 2, 4 ], [ 6, 8 ] ]
let s = arr2.map((x,i) => x.map(x => x*2))
console.log(s)


// 문제
let arr3 = [[1,2],[3,4]]
let arr4 = [[5,6],[7,8]]
// function solution1(A,B) {
//   return A.map((a,i) => a.map((b, j) => b + B[i][j]));
// }
// console.log(solution1(arr1, arr2))

// arr1의 배열값을 받아서 ( [1,2], [2,3] ) 그걸 다시 map으로 하여 1,2,3,4 를 만들어주고
// b가 1일때 자리가 0의 0번이니 i = 0 j = 0 이 들어가서 B[0][0]을 꺼내와서 더해준다  나머지도 같음
// [[6,8],[10,12]]
function solution2(A,B) {
  return A.map((a,i) => a.map((b,j) => {
    console.log("Value1: " + b)
    console.log("Value2: " + B[i][j])
    return b + B[i][j]
  }));
}
console.log(solution2(arr1, arr2))

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/map

 

Array.prototype.map() - JavaScript | MDN

map() 메서드는 배열 내의 모든 요소 각각에 대하여 주어진 함수를 호출한 결과를 모아 새로운 배열을 반환합니다.

developer.mozilla.org

 

주특기 들어갔을때 오류

 

performing an update on the path '_id' would modify the immutable field '_id' 오류

 

https://stackoverflow.com/questions/56350530/performing-an-update-on-the-path-id-would-modify-the-immutable-field-id

 

Performing an update on the path '_id' would modify the immutable field '_id'

I am trying to UPDATE fields in mongo database. However, I get the hollowing error. MongoError: Performing an update on the path '_id' would modify the immutable field '_id' My Code to update...

stackoverflow.com

 

E11000 duplicate key error collection: spa_board.posts index: postsId_1 dup key 오류

 

https://stackoverflow.com/questions/24430220/e11000-duplicate-key-error-index-in-mongodb-mongoose

 

E11000 duplicate key error index in mongodb mongoose

Following is my user schema in user.js model - var userSchema = new mongoose.Schema({ local: { name: { type: String }, email : { type: String, require: true, unique: true }, ...

stackoverflow.com

 

MongoDB Compass 설치 후 안되는 오류

 

-> 최시 버전 다운 받을시 cmd에서 안되는 경우가 있기 때문에 만약 최신버전이면 다운그레이드 적용

 

몽구스 오류로 인해 터미널에서 안돌아가는 오류

 

-> node -v 했을 경우, 이 글을 작성한 기준으로 17버전이면, locolhost:27017을 못받아 오는 경우가 있어서 127.0.0.1로 변경

728x90

'WIL' 카테고리의 다른 글

10.17 ~ 10.23 WIL  (0) 2022.10.22
10.10 ~ 10.16 WIL  (0) 2022.10.19
10.03~10.09 WIL  (0) 2022.10.08
9.19~9.25 WIL  (0) 2022.09.25