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

219
Views
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 answers
Answer question

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 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!