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

195
Views
convert array layout while removing duplicate values

I need to take serviceType from my array and convert it to the following layout keeping only the unique values.

[ { "Label": "Cars Now" "Value": "Cars Now" }, { "Label": "Vans Now" "Value": "Vans Now" }, ]

I've got the unique values below, but I can't work out how to convert it to the layout I need.

const inputArray = [
{
"serviceType": "Cars Now",
"applictionType": "Direct",
"wheelType": "4x4"
},
{
"serviceType": "Cars Now",
"applictionType": "web",
"wheelType": "2x4"
},
,
{
"serviceType": "Vans Now",
"applictionType": "Direct",
"wheelType": "2x4"
}
]

let tempArr = inputArray.map(row => row.serviceType) // map extracts the serviceType
           .filter(service => service); // filter removes any undefined/empty values

newServiceSet = [...new Set(tempArr)] // only keeps unique values

console.log(newServiceSet)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Turn the Set of serviceTypes into an an array of strings, then map to turn each string into an object. You also need to filter out the empty space in the array.

const inputArray = [
{
"serviceType": "Cars Now",
"applictionType": "Direct",
"wheelType": "4x4"
},
{
"serviceType": "Cars Now",
"applictionType": "web",
"wheelType": "2x4"
},
,
{
"serviceType": "Vans Now",
"applictionType": "Direct",
"wheelType": "2x4"
}
];
const serviceTypes = new Set(inputArray.filter(Boolean).map(({ serviceType }) => serviceType));
const output = [...serviceTypes].map(label => ({ label, value: label }));
console.log(output);

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!