How to filter and sort an array of objects based on another array

Amanda Santos :

I have an array of users that I need to be filtered and ordered according to another array "filter". Reading the code bellow you can see the expected result.

const users = [ 
  { key: 'abc', name: 'Anna', age: 22, gender: 'F' }, 
  { key: 'def', name: 'John', age: 25, gender: 'M' },
  { key: 'ghi', name: 'Mary', age: 27, gender: 'F' },
  { key: 'jkl', name: 'Joe',  age: 30, gender: 'M' } 
] 

const filter = [ 
  { key: 'jkl' },
  { key: 'def' },
  { key: 'abc' },
] 

// Here is the expected result:
const expected_result = [ 
  { key: 'jkl', name: 'Joe',  age: 30, gender: 'M' },
  { key: 'def', name: 'John', age: 25, gender: 'M' },
  { key: 'abc', name: 'Anna', age: 22, gender: 'F' }
] 

Please consider that the "key" attribute is unique. Does anyone know how I can do this? If you can also explain the logic behind your code I would appreciate it. I'm a begginer and I'm struggling with how to use the JavaScript functions to do this.

Thanks in advance!

Nick Parsons :

You can convert your first array of users to a Map. A Map is similar to an object, however, it has some differences (I'm using a Map here as it's easier to construct from an array compared to an object).

The Map would have the following shape:

Map {
  "abc": { key: 'abc', name: 'Anna', age: 22, gender: 'F' }, 
  "def": { key: 'def', name: 'John', age: 25, gender: 'M' },
  "ghi": { key: 'ghi', name: 'Mary', age: 27, gender: 'F' },
  "jkl": { key: 'jkl', name: 'Joe',  age: 30, gender: 'M' } 
}

In the code below, the above Map is stored in a variable called lut (short for look-up-table). Doing lut.get("def") will return the object stored at the key "def", in this case that object is:

{ key: 'def', name: 'John', age: 25, gender: 'M' }

JavaScript engines have optimized this look-up to be incredibly efficient, so creating a Map like this can help improve the overall scalability, meaning it will be efficient if you have many users.

Since order matters in your output, you can use .map() on your filter array (which determines the order) to convert each object with the name property to the object in the Map we built. To perform the "conversion" you can return the new object you want, which you can obtain from your Map using .get(o.key).

See working example below:

const users = [{ key: 'abc', name: 'Anna', age: 22, gender: 'F' }, { key: 'def', name: 'John', age: 25, gender: 'M' }, { key: 'ghi', name: 'Mary', age: 27, gender: 'F' }, { key: 'jkl', name: 'Joe',  age: 30, gender: 'M' }]; 
const filter = [{ key: 'jkl' }, { key: 'def' }, { key: 'abc' }]; 
const expected_result = [{ key: 'jkl', name: 'Joe',  age: 30, gender: 'M' }, { key: 'def', name: 'John', age: 25, gender: 'M' }, { key: 'abc', name: 'Anna', age: 22, gender: 'F' }];

const lut = new Map(users.map(o => [o.key, o]));
const result = filter.map(f => lut.get(f.key));
console.log(result);

If one of your objects in the filter can have a key for a user which isn't in the users list, the above approach will transform that filter object into "undefined". If you want to ignore it, you can use .filter() before you map. Using .filter() will remove all filter objects which don't have a key within the map:

const users = [{ key: 'abc', name: 'Anna', age: 22, gender: 'F' }, { key: 'def', name: 'John', age: 25, gender: 'M' }, { key: 'ghi', name: 'Mary', age: 27, gender: 'F' }, { key: 'jkl', name: 'Joe',  age: 30, gender: 'M' }]; 
const filter = [{ key: 'nonUserKey' }, { key: 'def' }, { key: 'abc' }]; 
const expected_result = [{ key: 'jkl', name: 'Joe',  age: 30, gender: 'M' }, { key: 'def', name: 'John', age: 25, gender: 'M' }, { key: 'abc', name: 'Anna', age: 22, gender: 'F' }];

const lut = new Map(users.map(o => [o.key, o]));
const result = filter.filter(o => lut.has(o.key)).map(f => lut.get(f.key));
console.log(result);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to sort an array of objects based on another?

Filter array of objects based on another array of objects

filter an array of objects based on another array of objects

How to sort array of objects keys based on another array keys position

How to filter array of objects with based on value from another array

Sort an array of objects based on another array of ids

Filter array of objects based on another array in data

Filter array of objects based on values in another array

Filter array of objects based on another array of arrays

How to filter an array of objects based of attribute from another object in Angular?

Javascript: Sort an array of objects based on another array of objects

Sort array of objects based on another array of objects key

Sort array of objects based on order of another array of objects

Angular - filter an array of objects based on values in another array of objects

JavaScript Filter array of objects based on property of another array of objects

Filter array of objects based on another array of objects in javascript

Filter array of objects based on criteria in another array of objects

How to sort an Array based on another Array in JS

How to filter an array based on words in another array?

How to arrange an array with objects based on another array?

Sort array of objects based on best matching another array

Vue/Javascript - Sort array of objects based on its existence in another array

Sort Array of Objects based on position of value in another Array of Strings

How to reduce an array of objects inside an array of objects based on another array

Sort array of objects by another array

typescript- filter array of objects based on another array

How to filter an array with complex nested objects based on another array of key/value pairs?

How do I filter an array of objects with MongoDB Object IDs based on another array of Object IDs?

How to filter items from array of objects based on another array of strings using javascript?