Q:

JavaScript Add key to each value in array

I have this array below that consists of a simple array. What i'm trying to accomplish is to put a key id in front of every array value to achieve something like this ["id:a", "id:b","id:c","id:d"] is there a easy way to accomplish this? any help would be greatly appreciated thank you.

var test = ['a','b','c','d']

A:

You can use .map():

var test = ['a', 'b', 'c', 'd'];

function setID(item, index) {
  var fullname = "id: " + item;
  return fullname;
}

var output = test.map(setID);
console.log(output);

A:

Use Array.from! It's really simple and faster.

var test = ['a', 'b', 'c', 'd'];
var newTest = Array.from(test, val => 'id: '+ val);
console.log(newTest);

