1

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;
});
2
  • So you want it to be removed from the original array? Commented May 9, 2018 at 11:52
  • no i dont want it to remove from original array Commented May 9, 2018 at 12:00

4 Answers 4

6

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

Sign up to request clarification or add additional context in comments.

4 Comments

but i want remove item from the list in an immutable way and want on the based of other array
@tapandave where is your other array in question? And where in the question you have mentioned immutable way?
i have mentioned below that based on other array that is ['match_number','id'] i want to remove keys from the list
@tapandave cool, check the edited answer. I have added another snippet there. Let me know if there is still some issues with that.
2

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

Comments

1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.