0

I have the following array of string that needs to be turn into array of object with the keys value and label

languages: ["Arabic", "Irish"]
           0:"Arabic"
           1:"Irish"
           length: 2

If I wanted to make this array into an array of object like the following:

languages = [
    { value: 'Arabic', label: 'Arabic' }
    { value: 'Irish', label: 'Irish' }
]

How will I do that? Can someone please help?

1

1 Answer 1

2

You can use Array.map():

var languages = ["Arabic", "Irish"];
var res = languages.map(item => ({'value':item, 'label':item}));
console.log(res);

You can also use Array.forEach() as:

var languages = ["Arabic", "Irish"];
var res = [];
languages.forEach(item => res.push({'value':item, 'label':item}));
console.log(res);

Sign up to request clarification or add additional context in comments.

6 Comments

Sorry, my bad I accidently clicked on the downvote button. I was trying to click on the accepted checkmark
It's saying I have to wait 10 mins before I can accept it
@SamWhite oh you can do it later
Sure, will. Thank you once again.
and will also accept it in 3 mins
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.