Efficient way to separate an array of objects based on another array

Owenn

For example I have an array of objects and an array as such:

const arrayObj = [
    {
        id: 1,
        name: "user1",
    },
    {
        id: 2,
        name: "user2",
    },
    {
        id: 3,
        name: "user3",
    },
]

const array = ["user1", "user2"]

How is it I'm able to separate arrayObj into two arrays based on array as such:

const array1 = [
    {
        id: 1,
        name: "user1",
    },
    {
        id: 2,
        name: "user2",
    },
]

const array2 = [
    {
        id: 3,
        name: "user3",
    },
]

I was thinking maybe something like this:

const filteredArray = arrayObj.filter((el) => {
  return array.some((f) => {
    return f === el.name;
  });
});

But is there a more efficient / quicker way?

CertainPerformance

Unless the arrays you're dealing with are huge, your current code is fine.

If the arrays are huge and the current code is too slow, put the names into a Set and check Set.has instead of Array.some - Set.has is much faster when there are many elements in the Set.

const userSet = new Set(array);
const usersInUserSet = arrayObj.filter(user => userSet.has(user.name));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Most efficient way to remove objects from array based on key value objects in another array

Is there an efficient way to get an array of objects from another array of objects

Javascript: Filter array of objects based on array values in an efficient way

Efficient way to merge two array of objects in javascript based on similarities?

Efficient way of alternating sort of array of objects based on property

Efficient way to group objects in an Array

Add JSON object from array of objects into another complex array of objects using JavaScript in efficient way

Filter Array of NSDictionaries Based on Objects in Separate Array

How to separate an array based on another array

Filter array of objects based on another array of objects

Creating array of objects based on another array of objects

filter an array of objects based on another array of objects

Efficient way of selecting elements of an array based on values from another array in Python?

Efficient way to swap property values in array of objects

Efficient way to merge array of objects with similar data

Restructuring JavaScript array of objects in the most efficient way

A more efficient way to rearrange Array of Objects?

More efficient way search for multiple objects in array

break array of objects into separate arrays based on a property

Sort an array of objects based on another array of ids

Filter array of objects based on another array in data

Removing objects from an array based on another array

Updating array based on newer objects in another array

Filter array of objects based on values in another array

Filter array of objects based on another array of arrays

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

Swift: Most efficient way to split array of objects into multiple arrays based on object property?

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

Efficient way to get unique objects from array of objects, by object property?