0

I am trying to remove objects from array based on value which is not matching.

This is my items array:

var items = [
  {"id":"88","name":"Lets go testing"},
  {"id":"99","name":"Have fun boys and girls"},
  {"id":"108","name":"You are awesome!"}
];

var arr=["88","108"];

Here I am able to removing objects from array based on matching values..But I want to keep matching value objects and need to remove unmatched objects.

This is how I'm removing matching objects from array.

for(let i in arr) {
  items = items.filter(function(item) {
    return item.id !== arr[i];
  });
}

1
  • 1
    filter by value items.filter( item => arr.includes( item.id ) ) Commented Sep 27, 2019 at 4:33

5 Answers 5

2

You may use Array.filter along with Array.includes:

var items = [{"id":"88","name":"Lets go testing"},{"id":"99","name":"Have fun boys and girls"},{"id":"108","name":"You are awesome!"}];
var arr = ["88", "108"];

const result = items.filter(item => arr.includes(item.id));
console.log(result);

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

Comments

1

Use indexOf to determine the occurrence.

var items = [
  {"id":"88","name":"Lets go testing"},
  {"id":"99","name":"Have fun boys and girls"},
  {"id":"108","name":"You are awesome!"}
];

var arr=["88","108"];

const filteredItems = items.filter(item => {
  if (arr.indexOf(item.id)>-1) {
    return item
    }
})

console.log(filteredItems)

Comments

1
items = items.filter(function(item) {
    return arr.indexOf(item._id)!=-1
  });

Comments

1

You can convert your items array into a look-up table (a Map of ids which point to your actual object), and then .map() each on your arr. This approach is particularly useful if you have lots of data in items or arr as it is efficient:

const items = [{"id":"88","name":"Lets go testing"},{"id":"99","name":"Have fun boys and girls"},{"id":"108","name":"You are awesome!"}];

const lut = new Map(items.map(({id, ...r}) => [id, {id, ...r}]));
const arr = ["88", "108"];

const result = arr.map(id => lut.get(id));
console.log(result);

If you can have multiple items which have the same id, then you can use .reduce() with .flatMap():

const items = [{"id":"88","name":"Lets go testing"}, {"id":"99","name":"Have fun boys and girls"},{"id":"108","name":"You are awesome!"}];

const lut = items.reduce((acc, obj) => {
  acc[obj.id] = acc[obj.id] || [];
  acc[obj.id].push(obj);
  return acc;
}, {});
const arr = ["88", "108"];

const result = arr.flatMap(id => lut[id]);
console.log(result);

Comments

0

Solution -

var items = [
    {"id":"88","name":"Lets go testing"},
    {"id":"99","name":"Have fun boys and girls"},
    {"id":"108","name":"You are awesome!"}
];
var arr = ["88","108"];
items = items.filter( (item) => arr.includes(item.id) );

2 Comments

It's just a different approach
How is that so? Looks identical to me

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.