找相同
for (let i = 0; i < arr2.length; i++) {
for (let j = 0; j < arr1.length; j++) {
if(arr1[j].goods_id === arr2[i].goods_id){
newArr.push(arr1[j]);
}
}
}
console.log(newArr,'123sa')
去重
let hash = {};
newArr = newArr.reduce((preVal, curVal) => {
hash[curVal.cat_id] ? '' : hash[curVal.cat_id] = true && preVal.push(curVal);
return preVal
}, [])
找选中和未选中
const curListData = this.listData //当前接口的新数据
const selectedIds = val ? val.map(o=>o.goods_id):[] //ui框架提供的val当前选中的项 ids字段
const curListIds = curListData.map(o=>o.goods_id) //当前接口的新数据的ids字段
const noSelectedIds = curListIds.filter(o=>{ //循环,找出当前没有选中的ids
return selectedIds.indexOf(o) < 0
})
const noSelectedArr = [] //循环,找出当前没有选中的ids的完整数组
for(let i=0;i<noSelectedIds.length;i++){
for(let j=0;j<curListData.length;j++){
if(noSelectedIds[i] == curListData[j].goods_id){
noSelectedArr.push(curListData[j])
}
}
}
// console.log(val,'选中的')
// console.log(noSelectedArr,'未选中的')
if(val){
//先添加进gouwuche并去重
this.gouwuche = this.gouwuche.concat(selectedIds)
let hash = {}
this.gouwuche = this.gouwuche.reduce((preVal, curVal) => {
hash[curVal] ? '' : hash[curVal] = true && preVal.push(curVal);
return preVal
}, [])
//再删除未勾选
const newArr = this.gouwuche
for(let i=0;i<noSelectedIds.length;i++){
const index = newArr.indexOf(noSelectedIds[i])
if(index !== -1){
newArr.splice(index,1)
}
}
this.gouwuche = newArr
}else{
const newArr = this.gouwuche
for(let i=0;i<noSelectedIds.length;i++){
const index = newArr.indexOf(noSelectedIds[i])
if(index !== -1){
newArr.splice(index,1)
}
}
this.gouwuche = newArr
}
js 判断数组的对象中是否有某个值
const arr=[
{id:1,name:'a'},
{id:2,name:'b'},
{id:3,name:'c'}
]
arr.some(({id})=>id==2)
//返回true或者false 可以在vue的标签使用 v-if
js如何判断对象数组中是否存在某个对象
var res = arr.some(item=>{
if(item.name=='张三'){
return true
}
})
console.log(res) // 如果arr数组对象中含有name:'张三',就会返回true,否则返回false
if(res){ // 如果存在
// do something
}
网友评论