Filter out array using another array of objects

li97

I am having two arrays

const selected = [];
const current = [
  { id: 1, name: "abc" },
  { id: 2, name: "def" }
];
const result = []

I need to compare these two arrays and the result should only have the single entry instead of duplicates. In the above example result should have the following output.

Also items in the selected should be taken into consideration and should be in the beginning of the result

result = [
  { id: 1, name: "abc" },
  { id: 2, name: "def" }
];

Also when the input is following

const selected = [ {id:5, name: "xyz" }];
const current = [
  { id: 1, name: "abc" },
  { id: 2, name: "def" }
];
result = [[
  { id: 5, name: "xyz" },
  { id: 1, name: "abc" },
  { id: 2, name: "def" }
];

Also when the input is following

const selected = [ {id:1, name: "abc" }, {id:4, name: "lmn" }];
const current = [
  { id: 1, name: "abc" },
  { id: 2, name: "def" }
];
result = [[
  { id: 1, name: "abc" },
  { id: 4, name: "lmn" }
  { id: 2, name: "def" }
];

Note the comparison should be made using name field

Code that I tried

const res = [...(selected || [])].filter((s) =>
  current.find((c) => s.name === c.name)
);

Sandbox: https://codesandbox.io/s/nervous-shannon-j1vn5k?file=/src/index.js:115-206

Nina Scholz

You could get all items and filter the array by checking the name with a Set.

const
    filterBy = (key, s = new Set) => o => !s.has(o[key]) && s.add(o[key]),
    selected = [{ id: 1, name: "abc" }, { id: 1, name: "lmn" }],
    current = [{ id: 1, name: "abc" }, { id: 2, name: "def" }],
    result = [...selected, ...current].filter(filterBy('name'));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Filter array of objects with another array of objects

Filter javascript objects with another array

Filter Javascript array of objects with another array of objects

Filter out an array from another array

Use one array to filter out another array

filter array of objects by another array of objects

Using filter in a nested array of objects

Filter array of objects based on another array of arrays

JavaScript filter one array of objects using another array

Filter array of objects by another array with duplicate keys

Filter out an array from the duplicates in another array

Filter array of objects based on another array in data

JS filter array with objects by another array

JS Array of Objects filter out values in an Array

Filter out an object in array of objects

Trying to filter an array of objects with another array of objects

Filter an array of objects via another integer array

javascript : filter array of objects using another array as filter

Filter out existing objects in an array of objects

Filter array of objects inside another array in JS

Lodash to filter out an array of objects

I want to filter out an array of objects by ids in another array

filter array by integers by property of another array of objects

Filter array of objects based on values in another array

How to filter an array using matching objects from another array and push it to a new array?

filter array of objects by methods of another array of objects

Array of objects filter by another array values

filter an array of objects based on another array of objects

Filter array of objects based on another array of objects