0

I am using react-reselect to filter out some data. I have to different arrays of data which I have to check if they are matching or not. How can I do a filter with two different arrays and have to run map function on them

Here is my selector

export const AssignedUserSelector = createSelector(
  [EmployeeSelector],
  employee => {
    const freshData = newData.map(newlyAssign => newlyAssign);
   
    return employee.employees.filter(assign => assign.employeeId === newData);
  }
);

here freshdata is something like this ["2222","333", "4444"] and employee.employees is like this [{id:"222", name: "John"}, {id:"333", name: "Jane"}, {id:"5555", name: "Josh"}].

What I am trying to do is filter out employees as per the id received. How I can I achieve this in react.

2
  • Why are you mapping via the identity function? It does nothing. Commented Jul 12, 2018 at 15:19
  • Just for the sake of the question. I tried doing map inside the filter still doesnot work. Commented Jul 12, 2018 at 15:54

1 Answer 1

2

Not entirely sure what you're doing with your example code, but this is probably what you're looking for:

Multiple Ids

const employeeIds = ["2222", "333", "4444"];

const employee = {
  employees: [{ 
    id: "222", 
    name: "John"
  },{
    id: "333", 
    name: "Jane"
  },{
    id: "5555", 
    name: "Josh"
  }]
};

employee.employees.filter(({ id }) => !employeeIds.includes(id));

Single Id

const employeeId = "222";

const employee = {
  employees: [{ 
    id: "222", 
    name: "John"
  },{
    id: "333", 
    name: "Jane"
  },{
    id: "5555", 
    name: "Josh"
  }]
};

employee.employees.filter(({ id }) => id !== employeeId);
Sign up to request clarification or add additional context in comments.

3 Comments

Hi thankx for response, my question is I have two arrays and I want to map then filter out array1 id’s matching array2 id’s
I'm confused. Why do you need to map? Your current map does nothing but return the same collection as a new array. If your first array freshData is simply a collection of one or more ids ['111', '222'] and you want to compare that collection to the employees collection then you don't need to map freshData since you're trying to filter the employees collection based on freshData. Array.map and Array.filter will always return a new array (refs change) so all you'd need to do is filter the employees collection against the ids and return the new filtered array.
Given the examples above, what is your expected output from the code snippet I posted?

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.