I got a comma seperated list
var arr = [1,2,3,4]
and want it to be:
var new_arr = [
{x:1, y:2},
{x:3, y:4}
]
Struggeling how to get the key/value change done.
>Solution :
You could use Array.reduce() to return the desired result.
If the element index is even, add a new item to the result, if not skip it.
const input = [1,2,3,4]
const result = input.reduce((acc, el, idx, arr) => {
return (idx % 2) ? acc : [...acc, { x: el, y: arr[idx + 1]} ];
}, []);
console.log('Result:', result);
.as-console-wrapper { max-height: 100% !important; }