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
-
developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…epascarello– epascarello2021年02月22日 14:18:14 +00:00Commented Feb 22, 2021 at 14:18
-
This link has the answer you expectBruno Silva– Bruno Silva2021年02月22日 14:21:27 +00:00Commented Feb 22, 2021 at 14:21
1 Answer 1
Use Array.map()
const a1 = [1,2,3];
const b1 = a1.map(val => ({id: val}));
console.log(b1);
answered Feb 22, 2021 at 14:18
topdev87
8,7913 gold badges11 silver badges33 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Andreas
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)
lang-js