map-filter-reduce

Harry Jung·2023년 10월 11일
0

초기값 꼭 챙겨야함. 데이터 베이스 및 type.
1. 초기값이 null이면 저장 /
2. 입력 id값이 데이터베이스 id와 일치하는 객체가 있으면 findIndex -1 / 0
3. 일치하면 수정 / map함수 사용
4. 일치하지 않으면 추가 / unshift함수 사용
** type 에러때문에 시간이 많이 걸림 (id 가 없어서 type error가 뜬것을 잘 확인 못함)

결과치-------
초기값 : null
함수: if. findIndex. map

warehouse - create/edit - json - createinvoices

if(existJobs.invoicesMain == null){
         const newprocessHistory = await this.jobs.save({
           id: input.jobsId,
           invoicesMain: [
             { ...input, createdAt: new Date(),}
           ],
         })
       }else{
         console.log("NULL이 아님")
         // purchae Order 같은지 아닌지
         const findOne = existJobs.invoicesMain.findIndex(x=>x.poNumber === input.poNumber)
         if(findOne >= 0){
           console.log("업데이트 요청 기존", existJobs.invoicesMain)
           console.log("업데이트 요청 내용", input)
           const edit = existJobs.invoicesMain.map(x=>x.poNumber == input.poNumber ? x = {id: input.jobsId, ...input} : x = x)
           console.log("업데이트 할 객체 찾기", edit)
           existJobs.invoicesMain = edit
           await this.jobs.save(existJobs)

         }else{
           console.log("추가 요청", findOne)
             existJobs.invoicesMain.unshift({
               id: input.jobsId,
               ...input,
               createdAt: new Date(),
             })
           await this.jobs.save(existJobs);

         }
         
const family = []

const create = {id: 1, name: "harry", age: 43}
const create = {id: 2, name: "jane", age: 35},
const create = {id: 3, name: "jayden", age: 7},

const edit = {id: 1, name: "harry", age: 43}

없으면 저장
있으면
1. id값이 같으면 수정
2. id값이 다르면 unshift

결과값

const family = [ 
 {id: 1, name: "harry",  age: 43}, 
 {id: 2, name: "jane",  age: 35}, 
 {id: 3, name: "jayden",  age: 7}, 
 {id: 4, name: "henry",  age: 7}, 
 {id: 5, name: "robin",  age: 1} 
]

찾고 수정하고 저장하고

수정하는 방법(Json 덮어씌우기)
1. 찾기

const findOne = family.filter(x=>x.id == 1)
  1. 찾은 것 수정하고 나머지것들도 다 같이 객체로 가져오기
const newFamily =  family.map(x=> x.id == findOne[0].id ? x = findOne[0] : x = x )
profile
Dreamsoft

0개의 댓글