-2

I am having the below array

const a1 = [1,2,3];

and want to convert it into

const b1 = [{id:1},{id:2},{id:3}];

Is there a quick way in doing in javascript

wangdev87
  • 8,611
  • 3
  • 8
  • 31
maxspan
  • 13,326
  • 15
  • 75
  • 104
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map – epascarello Feb 22 '21 at 14:18
  • This [link](https://stackoverflow.com/questions/3473639/best-way-to-convert-string-to-array-of-object-in-javascript) has the answer you expect – Bruno Silva Feb 22 '21 at 14:21

1 Answers1

4

Use Array.map()

const a1 = [1,2,3];
const b1 = a1.map(val => ({id: val}));
console.log(b1);
wangdev87
  • 8,611
  • 3
  • 8
  • 31
  • A question that is solvable by a 5 second search? Imho -> Yes. That question shouldn't exist in the first place and doesn't deserve an answer other than a close vote (needs details or as duplicate because this will then definitely have been asked and answered before) – Andreas Feb 22 '21 at 14:38