Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

96
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda