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

99
Visualizações
combine all forEach of an object

I have an object of objects as below

data = {
  Sam: { details: [ /* array items */ ] },
  Jill: { details: [ /* array items */ ] },
  Bill: { details: [ /* array items */ ] },
}

Then I have the code below to filter the data based on the name

filteredData = {};

Object.keys(data).forEach((name) => {
  filteredData[name] = data[name];
});

I call this in a constructor as below to get the details of the particular name

vis.dataFilteredTemp = filteredData[vis.name];
vis.dataFiltered = vis.dataFilteredTemp["details"];

if vis.name is Jill, then the output is {details: Array(3)} & this works just fine.

Now, I want the details of all names with is filteredData method. How can I do it? The out put expected is {details: Array(13)}} if I do vis.dataFilteredTemp = filteredData[] (empty array is passed). 13 arrays because arrays of all names are added up.

Since I am working with d3.csv, I am unable to pass the data directly to the constructor. Can any one help?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Iterate over the Object.values, and use flatMap to merge each array's details.

const data = {
  Sam: { details: [1,2,3,4]},
  Jill: { details: [4,5,6] },
  Bill: { details: [7,8,9,10,11,12] }
};

function getData(data, name = '') {
  if (name) return { details: data[name].details };
  return { details: Object.values(data).flatMap(arr => arr.details) };
}

console.log(getData(data));
console.log(getData(data, 'Jill'));

about 4 years ago · Juan Pablo Isaza Relatório

0

If I understand well, you should use Array.reduce

const data = {
  Sam: { details: [1,2] },
  jenn: { details: [3,4] },
  kurt: { details: [6, 9 , 10, 16] }
};

const result = Object.values(data).reduce((acc = {details: []}, val) => {
  acc.details.push(...val.details);
  return acc;
});

console.log(result.details);

outputs : [1, 2, 3, 4, 6, 9, 10, 16]

fiddle

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