8

I am new to javascript and I am trying to remove multiple values from an array of objects.

Right now I can remove one object like this

if(obj.findObjectByKey(response, 'cat_id', 171) == true) {
    var catId = response.map(item => item.cat_id).indexOf(171);
}

In the code above I will remove the item with a id of "171" but now I have an array with multiple values that I would like to remove.

So how can I modify the code above to let me pass in an array of items that I want to remove from the list eg ['171', '172', '173, '174'];

0

4 Answers 4

28

If you can use ES6, try using JavaScript's filter and includes for this.

const nums = [1, 2, 3, 4, 5, 6];
const remove = [1, 2, 4, 6];

function removeFromArray(original, remove) {
  return original.filter(value => !remove.includes(value));
}

console.log(removeFromArray(nums, remove));

This will work just as well with strings.

const nums = ["1", "2", "3", "4", "5", "6"];
const remove = ["1", "2", "4", "6"];

function removeFromArray(original, remove) {
  return original.filter(value => !remove.includes(value));
}

console.log(removeFromArray(nums, remove));

This solution also has the added benefit of being immutable. Meaning that you are returning new data rather than morphing the old. Which is considered good practice.

https://wecodetheweb.com/2016/02/12/immutable-javascript-using-es6-and-beyond/

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

Comments

0

You just need to loop over that array, and check for the array element.

arrayToRemove = ['171', '172', '173', '174'];

for (i = arrayToRemove.length; i>=0; i--) {
   if(obj.findObjectByKey(response, 'cat_id', arrayToRemove[i]) == true) {
       var catId = response.map(item => item.cat_id).indexOf(arrayToRemove[i]);
       response.splice(catId, 1);
   } 
}

There are other ways to do this, but this is the fastest and most straight forward with least changes to your current code.

It's important to note that you should loop this from the end of the array to remove to the beginning so you can preserve indices of the other elements in the remaining array.

Comments

0
var users = [
  { user: 'barney', age: 36, active: true },
  { user: 'fred', age: 40, active: false },
  { user: 'travis', age: 37, active: true },
];
var app = [{ user: 'barney' }, { user: 'fred' }];
_.forEach(app, (v) => {
  users = _.reject(users, { user: v.user });
});

Comments

0
   var userList = [
      { name: 'abhijeet', city: 'pune', id: 1},
      { name: 'rohit', city: 'pune', id: 2},
      { name: 'vicky', city: 'deglur', id: 3},
      { name: 'abhilash', city: 'washim', id: 4},
      { name: 'sumit', city: 'nagpur', id: 5}
    ];

    var removeUsers = [
      { name: 'vicky', city: 'deglur', id: 3},
      { name: 'sumit', city: 'nagpur', id: 5}
    ];

    var idArray = removeUsers.map((user) => user.id);

    userList = userList.filter((item) => {
        return idArray.indexOf(item.id) === -1;
    });

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.