'Map over string array and create property's for an object in JavaScript

I know this is a basic question, but I am trying to create a data structure that is baffling me at the moment.

I have a string array like so:

let stringArray = ['Core', 'Regional'];

And I would like to create this data structure:

[
  {'key': 'Core', 'text': 'Core'},
  {'key': 'Regional', 'text': 'Regional'}
]

How can I .map over the stringArray to create the data structure above?



Solution 1:[1]

You can try this solution

let stringArray = ['Core', 'Regional'];

stringArray = stringArray.map(str => ({key: str, text: str}));

console.log(stringArray);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 EzioMercer