Home > Net >  Map over string array and create property's for an object in Javascript
Map over string array and create property's for an object in Javascript

Time:03-08

Apologies I know this is a basic question for Senior developers out there, 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'}
]

Please can somebody help me .map over the stringArray to create the datacstructure above? ^

CodePudding user response:

You can try this solution

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

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

console.log(stringArray);

  • Related