3

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']
Best Jeanist
  • 1,109
  • 4
  • 14
  • 34
  • use [map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) – klugjo Jul 17 '18 at 02:07
  • 1
    You can just add `"id:"` in front of each of your elements and iterate through. Although exactly what is this to be used for? It doesn't seem all that helpful. Seems like your intending to want to make this an object with keys rather than an array of strings. – Spencer Wieczorek Jul 17 '18 at 02:10
  • use map like this: text.map( x=> "id:"+x), look for browser support. – PresidentPratik Jul 17 '18 at 02:11

6 Answers6

7

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);
Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
  • *map* works and is a clear solution. Just want to add that it doubles the size of memory since you're holding two arrays in memory. That may or may not be important, but is something to consider. – vol7ron Jul 17 '18 at 02:12
  • 2
    @ObsidianAge That worked well but what i want to make it into something like this [{"id":"a"}, {"id":"b"}, {"id":"c"}, {"id":"d"}] – Best Jeanist Jul 17 '18 at 02:25
4

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);
Axel
  • 4,365
  • 11
  • 63
  • 122
1

Just iterate over the array using forEach and set the value:

var test = ['a','b','c','d']
test.forEach((v,i,arr)=>arr[i]=`id:${v}`)

console.log(test)

Of course a standard for loop works as well:

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

for ( var i=0, n=test.length; i<n; i++){
   test[i] = 'id:' + test[i]
}

console.log(test)
vol7ron
  • 40,809
  • 21
  • 119
  • 172
1

Unable to edit @ObsidianAge answer, so minor change:

If you need in the form that the OP asked (Array of objects)

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

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

var output = test.map(setID);
console.log(output);
Biaspoint
  • 505
  • 3
  • 19
0

Using map to create a new array and simply prepend the "id: " to the string,

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

var newTest = test.map(function(item){return 'id:' +item})

console.log(newTest); // gives ["id": "a","id": "b","id": "c","id": "d"];

console.log(newTest[1]); // gives 1d: b;
gavgrif
  • 15,194
  • 2
  • 25
  • 27
  • we do not want an object in array, just want to prepend id: to the string – D. Seah Jul 17 '18 at 02:15
  • sorry - misread it - you can still use the same - but with a string being return instead of an object - answer amended. – gavgrif Jul 17 '18 at 02:18
0

As @klugjo says use map, like this:

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

const map1 = array1.map(x => 'id:' + x);

console.log(map1);
D. Mayen
  • 424
  • 3
  • 8