Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Below is the array of objects I’ve: 

[

    {

        id: 1,

        name: 'bill'

    },

    {

        id: 2,

        name: 'ted'

    }

]

I want the below output in a simple one-liner:

[

    {

        value: 1,

        text: 'bill'

    },

    {

        value: 2,

        text: 'ted'

    }

]

Can anyone tell me how to do this?

1 Answer

0 votes
by (19.7k points)

Use () to wrap the object:

 

var arr = [{

  id: 1,

  name: 'bill'

}, {

  id: 2,

  name: 'ted'

}]

var result = arr.map(person => ({ value: person.id, text: person.name }));

console.log(result)

 

Interested in Java? Check out this Java tutorial by Intellipaat.  

Related questions

0 votes
0 answers
asked Feb 18, 2021 in Java by Harsh (1.5k points)
0 votes
1 answer
0 votes
1 answer
asked Oct 23, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer

Browse Categories

...