精简语法

1.两个数组 arrA,arrB, 判断arrB中的prop如果与arrA中的prop重复,则此项arrB覆盖arrA

let arrA = [{prop:1,name:'1',value:'aa'},{prop:2,name:'2',value:'bb'},{prop:3,name:'3',value:'cc'},{prop:4,name:'4',value:'dd'},{prop:5,name:'5',value:'ee'},]

let arrB = [{prop:6,name:'6',value:'ff'},{prop:7,name:'7',value:'gg'},{prop:8,name:'8',value:'hh'},{prop:9,name:'9',value:'jj'},{prop:5,name:'5',value:'kk'},]

for(const item of arrB){

  const index = this.arrA.findIndex(v => v.prop === item.prop){

    if(index>-1){

      this.arrA.splice(index,1,item)

    }else{

      this.arrA.push(item)

    }

  }  

}

2.判断两个数组中是否有重复的的prop,如果有重复的则给search属性赋为true

let arrA = [1,2,3,4,5]

let arrB = [{prop:6,name:'6',value:'ff'},{prop:7,name:'7',value:'gg'},{prop:8,name:'8',value:'hh'},{prop:9,name:'9',value:'jj'},{prop:5,name:'5',value:'kk'},]

for(const v of this.arrA){

  v.search = this.arrB.includes( v.prop )

}

 

posted @ 2021-09-28 20:57  就想来看看  阅读(28)  评论(0编辑  收藏  举报