我想从列表中完全删除一个项目,下面是列表
var list = [{id: 1, match_number: 1, name: "match1"},
{id: 2, match_number: 2, name: "match2"},
{id: 3, match_number: 3, name: "match3"},
{id: 4, match_number: 4, name: "match4"},
{id: 5, match_number: 5, name: "match5"}]
我想从列表中完全删除match_number
,并希望实现类似
var list = [{id: 1, name: "match1"},
{id: 2, name: "match2"},
{id: 3, name: "match3"},
{id: 4, name: "match4"},
{id: 5, name: "match5"}]
以下是我获得单项结果的代码,但我有一个数组,其中键给出了 ['match_number','id']
。在此基础上我想从主列表中删除项目。
我该如何解决这个问题?
//code for single item
var listitem = list.map((i) => {
const {match_number,...item} = i;
return item;
});
答案 0 :(得分:6)
在map
数组上使用list
修改现有的对象数组,使其不具有match_number
键。
var list = [
{id: 1, match_number: 1, name: "match1"},
{id: 2, match_number: 2, name: "match2"},
{id: 3, match_number: 3, name: "match3"},
{id: 4, match_number: 4, name: "match4"},
{id: 5, match_number: 5, name: "match5"}
];
list.map(obj => delete obj.match_number);
console.log(list);
对于您要删除的密钥数组以及不影响原始list
数组的密钥,您可以使用此代码,
var list = [
{id: 1, match_number: 1, name: "match1"},
{id: 2, match_number: 2, name: "match2"},
{id: 3, match_number: 3, name: "match3"},
{id: 4, match_number: 4, name: "match4"},
{id: 5, match_number: 5, name: "match5"}
];
var res = [];
var removeArray = ['match_number','id'];
list.forEach((obj) => {
let tempObj = {};
Object.keys(obj).forEach((key)=>{
if(removeArray.indexOf(key) === -1){
tempObj[key] = obj[key];
}
});
res.push(tempObj);
});
console.log(res);
console.log(list);
答案 1 :(得分:2)
要保留要删除的数组,例如下面的attrToRemove
数组,并使用delete
以及map
和forEach
var list = [
{id: 1, match_number: 1, name: "match1"},
{id: 2, match_number: 2, name: "match2"},
{id: 3, match_number: 3, name: "match3"},
{id: 4, match_number: 4, name: "match4"},
{id: 5, match_number: 5, name: "match5"}
];
var attrToRemove = ['match_number','id'];
list.map(obj => attrToRemove.forEach(val=> delete obj[val]));
console.log(list);

答案 2 :(得分:1)
您可以循环使用密钥并使用destructuring
和rest
将其删除:
var list = [
{id: 1, match_number: 1, name: "match1"},
{id: 2, match_number: 2, name: "match2"},
{id: 3, match_number: 3, name: "match3"},
{id: 4, match_number: 4, name: "match4"},
{id: 5, match_number: 5, name: "match5"}
];
var keys = ['match_number', 'id'];
list = list.map(item => {
var x, rest = item;
keys.forEach(key => {
({ [key]: x, ...rest } = rest);
});
return rest;
});
console.log(list);

答案 3 :(得分:0)
您还可以使用$.map()
使用delete
运算符获取所需结果。
<强>样本强>
var list = [{id: 1, match_number: 1, name: "match1"},
{id: 2, match_number: 2, name: "match2"},
{id: 3, match_number: 3, name: "match3"},
{id: 4, match_number: 4, name: "match4"},
{id: 5, match_number: 5, name: "match5"}]
let removeKeys = ['match_number','id'];
let result = $.map(list,(o)=>{
$.each(removeKeys, (i,v)=> delete o[v]);
return o;
},[]);
console.log(result);
.as-console-wrapper {max-height: 100% !important;top: 0;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
您还可以使用reduce()
数组方法来获取所需的结果。使用concat()
和delete
。
<强>样本强>
var list = [{id: 1, match_number: 1, name: "match1"},
{id: 2, match_number: 2, name: "match2"},
{id: 3, match_number: 3, name: "match3"},
{id: 4, match_number: 4, name: "match4"},
{id: 5, match_number: 5, name: "match5"}]
let removeKeys = ['match_number','id'];
let result = list.reduce((r,o)=>{
removeKeys.forEach(v=> delete o[v]);
return r.concat(o);
},[]);
console.log(result);
.as-console-wrapper {max-height: 100% !important;top: 0;}