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

202
Views
Reduce to map of type typescript

I have this configurations

type Option = {
  value: any,
  label: any
}

And

export interface Role {
  id: number
  name: string
}

Then I have Roles

const roles:Role[]:[
{id:1,name:'name1'},
{id:2,name:'name2'},
{id:3,name:'name3'}
]

I want to extract Option[] from that roles dataset. How do i do it in javascript?

In java I could do

roles.stream().map(role->new Option(role.id,role.name)).collect(collectors.toList());
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can just map() the roles to options:

type Option = {
  value: any;
  label: any;
}

interface Role {
  id: number;
  name: string;
}

const roles: Role[] = [
  {id: 1, name: 'name1'},
  {id: 2, name: 'name2'},
  {id: 3, name: 'name3'}
];

const options: Option[] = roles.map(({id, name}) => ({value: id, label: name}));

Playground link

about 4 years ago · Juan Pablo Isaza Report

0

Typescript Playground link

type Option = {
  value: any,
  label: any
}

interface Role {
  id: number
  name: string
}

const roles: Role[] = [
  { id: 0, name: "foo" },
  { id: 1, name: "bar" },
  { id: 2, name: "baz" },
]

const result: Option[] = roles
  .map(({ id, name }) => ({ label: id, value: name }));

console.log(result);

Result:

[LOG]: [{
  "label": 0,
  "value": "foo"
}, {
  "label": 1,
  "value": "bar"
}, {
  "label": 2,
  "value": "baz"
}]
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!