Javascript 如何从对象数组中删除项?
如何从对象数组中删除项? 有一个属性数组,根据该数组,有必要从中删除与数组不一致的项。 以下是一个例子:Javascript 如何从对象数组中删除项?,javascript,jquery,arrays,typescript,Javascript,Jquery,Arrays,Typescript,如何从对象数组中删除项? 有一个属性数组,根据该数组,有必要从中删除与数组不一致的项。 以下是一个例子: let selectedIsn = [10,15,20,30,40]; let arayObject = [{ isn:10, name:"Bolt" }, { isn:13, name:"marry" },{ isn:15, name:"a" },{ isn:18, name:"q" }, { isn:20, name:"marrys" },{
let selectedIsn = [10,15,20,30,40];
let arayObject = [{
isn:10,
name:"Bolt"
}, {
isn:13,
name:"marry"
},{
isn:15,
name:"a"
},{
isn:18,
name:"q"
}, {
isn:20,
name:"marrys"
},{
isn:25,
name:"aa"
},{
isn:30,
name:"qa"
}, {
isn:40,
name:"marrya"
},{
isn:55,
name:"sa"
},{
isn:68,
name:"qas"
}];
let deleteSelected = (q,selectedItems) => {
let arrayNew = q,
count=0;
for (var m = 0; m < q.length; m++) {
let index = selectedItems.indexOf(q[m]["isn"]);
if (index > -1) {
arrayNew.splice(m - count, 1);
count++;
}
}
return arrayNew;
}
deleteSelected(arayObject,selectedIsn);
让selectedIsn=[10,15,20,30,40];
设arayObject=[{
不是:10,
名称:“螺栓”
}, {
是不是:13,
姓名:“结婚”
},{
时间:15,
姓名:“a”
},{
18岁,
名称:“q”
}, {
是不是:20,
姓名:“结婚”
},{
不是:25,
名称:“aa”
},{
时间:30,
名称:“qa”
}, {
不是:40,
姓名:“玛丽娅”
},{
第55页,
姓名:“sa”
},{
编号:68,
名称:“qas”
}];
让deleteSelected=(q,selectedItems)=>{
设arrayNew=q,
计数=0;
对于(var m=0;m-1){
阵列新拼接(m-计数,1);
计数++;
}
}
返回arrayNew;
}
删除选定项(arayObject,selectedIsn);
不删除给定列表中的所有元素。我不明白怎么回事。像这样使用过滤器就行了
var result = arayObject.filter(item => selectedIsn.indexOf(item.isn)===-1);
只需使用这样的过滤器
var result = arayObject.filter(item => selectedIsn.indexOf(item.isn)===-1);
您可以使用
.filter
函数从数组中获取所选项目
let deselected = arayObject.filter(function (a) {
return selectedIsn.indexOf(a.isn) < 0;
});
let deselected=arayObject.filter(函数(a){
返回selectedIsn.indexOf(a.isn)<0;
});
您可以使用.filter
功能从数组中获取所选项目
let deselected = arayObject.filter(function (a) {
return selectedIsn.indexOf(a.isn) < 0;
});
let deselected=arayObject.filter(函数(a){
返回selectedIsn.indexOf(a.isn)<0;
});