-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,285
  • 3
  • 6
  • 30
maxspan
  • 12,194
  • 13
  • 68
  • 95
  • 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,285
  • 3
  • 6
  • 30
  • 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