score:0

Accepted answer

Looks like a perfect use of Array.prototype.reduce()

Looks like you're using ES5, so here's what that would look like as ES5

let res1 = items.reduce(function(acc, item){
  return acc.concat({
    "key": item.key,
    "values": item.values.map(function(value) { return [value.key, value.value] })
  })
}, []);

And here's what that would look like as ES6

let res2 = items.reduce((acc, item) => {
  return acc.concat({
    "key": item.key,
    "values": item.values.map((value) => [value.key, value.value])
  })
}, []);

Both of the above uses this array (which fixes a syntax error in your sample)...

let items = [{ "key": "path1", "values": [{ "key": 1, "value": "x" }, { "key": 2, "value": "y" }, { "key": 3, "value": "z" }] }, { "key": "path2", "values": [{ "key": 1, "value": "a" }, { "key": 1, "value": "b" }, { "key": 1, "value": "c" }] }];

I've setup a repl on repl.it here... https://repl.it/MsQu


Related Query

More Query from same tag