Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple arrays transformed to objects to one array with Node JS

For example I have this arrays:

a = [1, 2, 3];
b = ["a", "b", "c"];

I want to make one object from these arrays that would look like this:

c = [{
  a: 1,
  b: "a"
},
{
  a: 2,
  b: "b"
},
{
  a: 3,
  b: "c"
}];
like image 729
mesopotamija9 Avatar asked Apr 18 '26 06:04

mesopotamija9


1 Answers

You can use Array.map

let a = [1, 2, 3];
let b = ["a", "b", "c"];
let c = a.map((v,i) => ({a:v, b: b[i]}));
console.log(c);
like image 106
Nikhil Aggarwal Avatar answered Apr 20 '26 21:04

Nikhil Aggarwal