0

I need to find all employees(EmpID) with same projects(ProjectID), data will be dynamic, this array is for example.

[
  { EmpID: '143,', ProjectID: '12,', DateFrom: '2013-11-01,', DateTo: '2014-01-05\r' },
  { EmpID: '218,', ProjectID: '10,', DateFrom: '2012-05-16,', DateTo: 'NULL\r' },
  { EmpID: '143,', ProjectID: '10,', DateFrom: '2009-01-01,', DateTo: '2011-04-27' },
];

I want this array to be ->

[
  { EmpID: '218,', ProjectID: '10,', DateFrom: '2012-05-16,', DateTo: 'NULL\r' },
  { EmpID: '143,', ProjectID: '10,', DateFrom: '2009-01-01,', DateTo: '2011-04-27' },
];
3
  • 1
    Does this answer your question? How to filter array of objects by duplicate and property? Commented Nov 30, 2021 at 19:50
  • What is the business logic for duplicates - which one is chosen? The first one? Commented Nov 30, 2021 at 19:51
  • I want to find all Employees(EmpID) with same projects(ProjectId). Commented Nov 30, 2021 at 19:54

2 Answers 2

1

You should be able to use Array.reduce to accomplish this

// The "trick" is to pull out the first item and then use that as the match candidate
const [head, ...tail] = [{ EmpId: 1 }, { EmpId: 2 }, { EmpId: 1 }]

const output = tail.reduce(
  (acc, b) => {
    if (acc.map(o => o.EmpId).includes(b.EmpId)) {
      acc.push(b)
    }
    return acc
  },
  [head]
)
Sign up to request clarification or add additional context in comments.

Comments

1

Try something like this:

const projectEmployees = employees.filter( emp => emp.ProjectID === '12' );

See Array.filter() for the gory details.

Or... roll your own:

function selectEmployeesByProjectId( employees, id ) {
  let projectEmployees = [];
  for ( const emp of employees ) {
    if ( emp.ProjectID === id ) {
      projectEmployees.push(emp);
    }
  }
  return projectEmployees;
}

If your data is truly dynamic and you don't know its true shape, other than it's a list of objects, just make your function flexible, something like the following. [But you'll still need to know what property or properties to match against to do your filtering.

function selectMatchingObjects( listOfObjects, isWanted ) {
  return listOfObjects.filter( isWanted );
}

where isWanted is a function that accepts an object and returns a boolean true if the object is to be kept, or false if the object is to be discarded.

Once you have that you can do something like

function selectEmployeesByProject( employees , projectIdKey , projectId ) {
  const isWanted = emp = emp[projectIdKey] === projectId;
  return employees.filter( isWanted );
}

And then

const employees = getMySomeDynamicEmployees();
. . .
const projectEmployees = selectEmployeesByProject(employees, 'ProjectId', '10');

4 Comments

Yes, but the data in the array is dynamic and I don't know what it will be. I have to create a function for dynamic data.
Then update your question to be more clear. @dani
This is the easiest way to achieve it, as it skips looking up the property. In terms of dynamic data and project_id, you should be able to pick the project_id from the first item in the array and use that as the parameter instead
@dani — see my amended answer. If something like that doesn't work, you need to better explains exactly what it is you're trying to accomplish. No matter what you do, you will somehow have to (1) identify the property or properties to filter on, and (2) decide what value or combination of values identifies the keepers.

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.