14

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?

4 Answers 4

24

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);

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Yeldar, it worked perfecly fine for me! I will accept it as answer in about 5 minutes in case your post was the first one to come.
15

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);

5 Comments

how can I make it like this? [ { 1 : 2 } , { 3 : 4 } , { 5 : 6 } , { 7 : 8 } ]
@fazeelzama Replace {lat, lng} with {[lat]: lng}.
i need to do same thing but i have arrs.map is not a function
@Versifiction You should ask a new question and provide details with actual code. There is absolutely no way to know what you're doing with just that comment alone.
1

You can use Array#map to achieve this. Array#map passes each array element through a projection function and returns the values in a new array.

var data= [
    ['val1', 'val2'],
    ['val1', 'val2'],
    ['val1', 'val2'],
    ['valN', 'valN'],
];

var result = data.map(function(row) {
  return {
    lat: row[0],
    lng: row[1]
  };
});

console.log(result);

1 Comment

i need to do same thing but i have data.map is not a function
0

var array = [
    ['val1', 'val2'],
    ['val1', 'val2'],
    ['val1', 'val2'],
    ['valN', 'valN']
];

var arrayObject = array.map(function(item){ 
  return { lat: item[0], lng: item[1]};
});

console.log(arrayObject);

1 Comment

i need to do same thing but i have array.map is not a function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.