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

159
Visualizações
Matching ifferent forms of nested JS object / array data matching

I have the results of my MongoDB db query for two variables as follows :

foodelectricity [
  { food: 'dairy', electricity: 1 },
  { food: 'fruitAndVeg', electricity: 1 },
  { food: 'grains', electricity: 1 },
  { food: 'bakedGoods', electricity: 5 },
  { food: 'protein', electricity: 1 }
]

foodresults { bakedGoods: 1, dairy: 3, fruitAndVeg: 1, grains: 2, protein: 1 }

I need to multiyply the number values in foodresults times the electricity associated with that same item type in food electricity.

For example :

let helpValue = foodresults.bakedGoods = 1 * foodElectricity.food.bakedGoods = 5.

I not believe that MongoDB returns the array in the same order each time -- so some sort of key value matching is required.

I am having trouble figuring out how to find the helpValue.

Any help is deeply appreciated.

With thanks Karen

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

0

You could make a copy of foodresults and then loop over foodelectricity array and multiply electricity with matching value from the results object.

const foodelectricity = [{"food":"dairy","electricity":1},{"food":"fruitAndVeg","electricity":1},{"food":"grains","electricity":1},{"food":"bakedGoods","electricity":5},{"food":"protein","electricity":1}]

const foodresults = {
  bakedGoods: 1,
  dairy: 3,
  fruitAndVeg: 1,
  grains: 2,
  protein: 1
}

 const helpValues = { ...foodresults }
 foodelectricity.forEach(({ food, electricity }) => {
   helpValues[food] *= electricity
 })
 
 console.log(helpValues)

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