Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

93
Views
fillter arrays of objects

i have two arrays.

const department = [
  { id: '1', name: 'department1' },
  { id: '2', name: 'department2' },
];

const models = [
  {
    id: '23',
    name: 'model1',
    departments: [{ id: '1', name: 'department1' }],
  },
  {
    id: '54',
    name: 'model2',
    departments: [
      { id: '1', name: 'department1' },
      { id: '2', name: 'department2' },
    ],
  },
];

i need to render accordions with department names and accordion details with matching models names. My question is how to filter those arrays to get models

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

We can map through the departments array, and add a models property that equals the models array, but filtered only to the ones that contain a matching department id.

const departments = [
  { id: "1", name: "department1" },
  { id: "2", name: "department2" },
];

const models = [
  {
    id: "23",
    name: "model1",
    departments: [{ id: "1", name: "department1" }],
  },
  {
    id: "54",
    name: "model2",
    departments: [
      { id: "1", name: "department1" },
      { id: "2", name: "department2" },
    ],
  },
];

const getDepartmentsWithModels = () => {
  return departments.map((department) => {
    return {
      ...department,
      models: models.filter((model) => {
        const modelDepartmentIds = model.departments.map(({ id }) => id);
        return modelDepartmentIds.includes(department.id);
      }),
    };
  });
};

console.log(getDepartmentsWithModels());
// [ { id: '1', name: 'department1', models: [ [Object], [Object] ] },
//  { id: '2', name: 'department2', models: [ [Object] ] } ]```
about 4 years ago · Juan Pablo Isaza Report

0

There are two solutions.

Using Array.reduce() --> returns an object where the key is department name and value is an array of the names of matching models:

let data1 = models.reduce((res, curr) => {
    curr.departments.forEach(dep => {
        if (!res[dep.name]) {
            res[dep.name] = [curr.name]
        } else {
            if (!res[dep.name].includes(curr.name)) {
               res[dep.name].push(curr.name); 
            } 
        }
    })
    return res;
}, {});

 

Using map and filter --> returns an array of kind: [{department: [names of the models]},...]

let data2 = department.map(dep => {
    let matchingModels = models.filter(model => {
        return model.departments.filter(modDep => {
            return modDep.name === dep.name;
        }).length > 0;
    }).map(mod => {
         return mod.name;
    });

    return {
        department: dep.name,
        models: matchingModels
    }
});
about 4 years ago · Juan Pablo Isaza Report

0

I've built some code, which iterates over the departments. For each department it iterates the models and for each model it checks if the department is within the model departments.

const department =
[
    { id: '1', name: 'department1' },
    { id: '2', name: 'department2' }
]

const models =
[
    { 
        id: '23', 
        name: 'model1',
        departments: [{ id: '1', name: 'department1' }] 
    },
    { 
        id: '54',
        name: 'model2',
        departments: [{ id: '1', name: 'department1' },{ id: '2', name: 'department2' }]
    }
]

department.forEach( dep => {
    console.log(`Department: ${dep.name}`)
    models.forEach(model => {
        if (model.departments.find(modelDep => dep.id===modelDep.id)) {
            console.log(`  Model: ${model.name}`) 
        }
    })
})

If you could change your data objects, then your code could be much smoother.

I've changed your data objects slightly by just reducing the departments in a model to be an array of department id's. This code iterates over the departments. For each department it filters the models and iterates over the filtered models to output them to the console. This is lesser code and provides much better performance.

const department =
[
    { id: '1', name: 'department1' },
    { id: '2', name: 'department2' }
]

const models =
[
    { 
        id: '23', 
        name: 'model1',
        departments: ['1'] 
    },
    { 
        id: '54',
        name: 'model2',
        departments: ['1', '2']
    }
]

department.forEach( dep => {
    console.log(`Department: ${dep.name}`)
    models.filter(model => model.departments.includes(dep.id)).forEach(model => {
        console.log(`  Model: ${model.name}`)
    })
})
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!