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

224
Visualizações
Map javascript object with array using lodash

I am new using lodash and I would like to solve the following scenario in a clean way using lodash instead a for statement.

The array "data" should be used to search in the JS object "Categories" to find each element, return the value and generate an array as is shown in the code below. #If the array contain a element that doesn't exist in the JS object, it should return a default value.

var categories = {
  "A" : "LOW",
  "B" : "LOW",
  "C" : "MEDIUM",
  "D" : "MEDIUM",
  "E" : "HIGH"
}

var data = ["A", "B", "C", "Unexpected"]

var defaultValue = "VERYLOW"



expected result:
["LOW", "MEDIUM", "VERYLOW"]

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

0

This isn't what you asked for, but Lodash is just an abstraction for vanilla JS. For what it's worth, this is pretty clean (and doesn't use for-in)

[...new Set(data.reduce((b,a) => ([...b, categories[a] ?? defaultValue]),[]))]

let categories = { "A" : "LOW", "B" : "LOW", "C" : "MEDIUM", "D" : "MEDIUM", "E" : "HIGH" }
let data = ["A", "B", "C", "Unexpected"], defaultValue = "VERYLOW"

let result = [...new Set(data.reduce((b,a) => ([...b, categories[a] ?? defaultValue]),[]))]

console.log(result)

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