Skip to main content
Tweeted twitter.com/StackCodeReview/status/1047637528604553216

I have an array of arrays and I want to create an array of objects from it. There are custom properties to which I want to associate the indexsub-array values. I have the following array:

var testArray = [[1,5,10], [10,20,7], [11,10,25]];

I am modifying it to create the following

[{
    coordinates: [1, 5],
    reach: 10,
    power: 0
},
{
    coordinates: [10, 20],
    reach: 7,
    power: 0
},
{
    coordinates: [11, 10],
    reach: 25,
    power: 0
}]

For this I have written the following code.

function modifyArray(array) {
    return array.map(array => {
        return Object.assign({}, { coordinates: array.slice(0, 2), reach: array[2], power: 0 });
    });
}

}

I get the desired result. My question is if this is a good way to modify this type of data performance wise, since the function has two return statements. How can I improve on this code?

I have an array of arrays and I want to create an array of objects from it. There are custom properties to which I want to associate the index values. I have following array

var testArray = [[1,5,10], [10,20,7], [11,10,25]];

I am modifying it to create the following

[{
    coordinates: [1, 5],
    reach: 10,
    power: 0
},
{
    coordinates: [10, 20],
    reach: 7,
    power: 0
},
{
    coordinates: [11, 10],
    reach: 25,
    power: 0
}]

For this I have written the following code.

function modifyArray(array) {
  return array.map(array => {
    return Object.assign({}, { coordinates: array.slice(0, 2), reach: array[2], power: 0 });
});

}

I get the desired result. My question is if this is a good way to modify this type of data performance wise, since the function has two return statements. How can I improve on this code?

I have an array of arrays and I want to create an array of objects from it. There are custom properties to which I want to associate the sub-array values. I have the following array:

var testArray = [[1,5,10], [10,20,7], [11,10,25]];

I am modifying it to create the following

[{
    coordinates: [1, 5],
    reach: 10,
    power: 0
},
{
    coordinates: [10, 20],
    reach: 7,
    power: 0
},
{
    coordinates: [11, 10],
    reach: 25,
    power: 0
}]

For this I have written the following code.

function modifyArray(array) {
    return array.map(array => {
        return Object.assign({}, { coordinates: array.slice(0, 2), reach: array[2], power: 0 });
    });
}

I get the desired result. My question is if this is a good way to modify this type of data performance wise, since the function has two return statements. How can I improve on this code?

Source Link
Saqib S
  • 165
  • 3

Modify array of arrays to create custom array of objects

I have an array of arrays and I want to create an array of objects from it. There are custom properties to which I want to associate the index values. I have following array

var testArray = [[1,5,10], [10,20,7], [11,10,25]];

I am modifying it to create the following

[{
    coordinates: [1, 5],
    reach: 10,
    power: 0
},
{
    coordinates: [10, 20],
    reach: 7,
    power: 0
},
{
    coordinates: [11, 10],
    reach: 25,
    power: 0
}]

For this I have written the following code.

function modifyArray(array) {
  return array.map(array => {
    return Object.assign({}, { coordinates: array.slice(0, 2), reach: array[2], power: 0 });
});

}

I get the desired result. My question is if this is a good way to modify this type of data performance wise, since the function has two return statements. How can I improve on this code?