基于其他数组从javascript中的数组中删除项目

时间:2018-05-09 11:44:01

标签: javascript jquery arrays arraylist javascript-objects

我想从列表中完全删除一个项目,下面是列表

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;
});

4 个答案:

答案 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以及mapforEach



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)

您可以循环使用密钥并使用destructuringrest将其删除:



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)

的Jquery + ES6

您还可以使用$.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>

ES6

您还可以使用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;}