Samantha J Samantha J - 7 months ago 11
Javascript Question

Is there a simple way I can populate an Array with objects containing an id and name from A to Z?

Currently my code is doing this:

wordLetterSelect = [
{
id: 'A',
name: 'A'
}, {
id: 'B',
name: 'B'
}
];


But I need to do it for every character in the alphabet. Is there a simple way that I could do this without just repeating one character after the other in the same way as I have done for A and B?

Update

The possible duplicate question does something similar but does not do what I am looking for. Hope someone can help with this. Thanks

Answer

Same as the other answers but using map:

var wordLetterSelect = 'abcdefghijklmnopqrstuvwxyz'.split('').map(function(param){ 
   return obj = {
        id: param.toUpperCase(),
        name: param.toUpperCase()
   };
});