I have an array of strings e.g. const array1 = ["124542", "112244", "112245", "112246"];
I want to create below results:
const array2 = [
{
"comparableId": "124542",
"comparatorId": "112244"
},
{
"comparableId": "112244",
"comparatorId": "112245"
},
{
"comparableId": "112245",
"comparatorId": "112246"
}
]
In this array2, 1st index value from array1 is with 2nd value, 2nd value with 3rd and 3rd with 4th and so on. This array1 is dynamic and can have just 1 value e.g. const array1 = ["124542"]; and can have any number of values.
I have tried using Ecma Script reduce and map methods.
const array2 = array1.map((value, index) => {
return {
comparableId: value,
comparatorId: array1[index + 1] || '',
};
});
Is there some other good way to do it ?