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

后端 未结 4 848
北荒
北荒 2020-11-27 08:07

I have following array:

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

N represents the fact that the

4条回答
  •  佛祖请我去吃肉
    2020-11-27 08:42

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

提交回复
热议问题