Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have following array:

[
    [val1, val2]
    [val1, val2]
    [val1, val2]
    [valN, valN]
]

N represents the fact that these arrays are not limited, i.e I never know how much of them do I store. What I try to accomplish is converting that array to array of objects with keys lat and lng and I expect so have final result as following so I can use it for further needs:

[
    {
        lat: val1,
        lng: val2
    },
    {
        lat: val1,
        lng: val2
    },
    {
        lat: valN,
        lng: valN
    }
]

I found a function to convert these inner arrays to objects and it looks like this:

objectify(array) {
    return array.reduce(function(result, currentArray) {
        result[currentArray[0]] = currentArray[1];
        return result;
    }, {});
}

It works but output looks like this:

[
    {val1: val1, val2: val2}
    {val1: val1, val2: val2}
    {valN: valN, valN: valN}
]

and that's not what I'm looking for, because I really need these lat and lng to be keys of an object. How can I solve such issue?

like image 207
Michael Spiegelberg Avatar asked Jul 19 '17 09:07

Michael Spiegelberg


People also ask

How do I turn an array into an array of objects?

To convert an array to an object, use the reduce() method to iterate over the array, passing it an object as the initial value. On each iteration, assign a new key-value pair to the accumulated object and return the result. Copied! const arr = ['zero', 'one', 'two']; const obj4 = arr.

Can you use includes () with an array in JavaScript?

You can use the includes() method in JavaScript to check if an item exists in an array. You can also use it to check if a substring exists within a string. It returns true if the item is found in the array/string and false if the item doesn't exist.

Can you have an array of arrays in JavaScript?

JavaScript does not provide the multidimensional array natively. However, you can create a multidimensional array by defining an array of elements, where each element is also another array. For this reason, we can say that a JavaScript multidimensional array is an array of arrays.

How do you populate an array in JavaScript?

Array.fill For example, if we want to set up an array with ten slots and populate it with the string “hello” we'd write some code like this: let filledArray = new Array(10). fill('hello'); This method works great for immutable values like numbers, strings, and booleans.


2 Answers

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);
like image 85
Yeldar Kurmangaliyev Avatar answered Sep 28 '22 01:09

Yeldar Kurmangaliyev


Using ES6 you can write this in a very concise way:

var arrs = [
    [1, 2],
    [3, 4],
    [5, 6],
    [7, 8]
];
const locations = arrs.map(([lat, lng]) => ({lat, lng}));
console.log(locations);
like image 38
str Avatar answered Sep 28 '22 01:09

str