tapan dave
tapan dave

Reputation: 303

remove item from array in javascript based on other array

I want to remove completely an item from the list, below is the 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"}]

I want remove match_number completely from the list, and want to achieve like

var list = [{id: 1, name: "match1"},
     {id: 2, name: "match2"},
     {id: 3, name: "match3"},
     {id: 4, name: "match4"},
     {id: 5, name: "match5"}]

Below is the code where I achieved the result for single item, but I have an array where key are given like ['match_number','id']. on the basis of this I want to remove item from the main list.

How can I solve this?

//code for single item
var listitem = list.map((i) => {
    const {match_number,...item} = i;
    return item;
});

Upvotes: 1

Views: 87

Answers (4)

Narendra Jadhav
Narendra Jadhav

Reputation: 10262

Jquery+ES6

You can also use $.map() to get required result using delete operator.

DEMO

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

You could also use reduce() method of array to get the required result. With using of concat() and delete.

DEMO

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

Upvotes: 0

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

Use map on the list array to modify the existing array of objects to not to have match_number key.

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

For your array of keys which you want to remove and also for not affecting the original list array you can go with this,

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

Upvotes: 6

Faly
Faly

Reputation: 13356

You can loop through your keys and remove them by using destructuring and 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);

Upvotes: 1

Ramesh
Ramesh

Reputation: 13266

Keep the attributes to remove in an array like attrToRemove below and use delete along with map and 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);

Upvotes: 2

Related Questions