Javascript – convert array of arrays into array of objects with prefilled values

You can simply use Array.prototype.map to project an array into another one by applying a projection function:

var arrs = [
    [1, 2],
    [3, 4],
    [5, 6],
    [7, 8]
];

var objs = arrs.map(x => ({ 
  lat: x[0], 
  lng: x[1] 
}));

/* or, using the older "function" syntax:

var objs = arrs.map(function(x) { 
  return {
    lat: x[0], 
    lng: x[1] 
  };
);

*/

console.log(objs);

Leave a Comment