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
Source: stackoverflow.com
Related Query
- Convert sub-array of objects with key-value pairs into array of arrays
- Javascript: Array of dictionaries, get all key value pairs from one dictionairy with condition
- Populating Nested Arrays with Key value pairs
- d3.selectAll enter exit of nested key value pairs -- aka: how do array selections work?
- Count number of items in an array of objects with a specific property value (JavaScript)
- Rearranging an array with key value
- Convert array of values to array of objects (key-value) pairs in JavaScript
- How to manipulate JSON objects into an array with javascript
- javascript, convert an array of objects to an array of arrays vertically
- d3.js enter() update() exit() pattern, but with custom comparison of key value pairs
- D3 JS append a new text box with 3 new key value pairs
- Convert JSON made by PHP "json_encode", into array to plot those values with D3
- How to get maximum value from an array of objects to use in d3.scale.linear().domain()
- D3: use nest function to turn flat data with parent key into a hierarchy
- D3.js Selecting min/max value from an array of arrays (nesting d3.extent)
- d3: A sub array of objects
- How to get biggest array element based on his second sub array value
- Turn a JSON array of arrays into <ul> and <li> elements
- How to count and convert an array with specific condition javascript
- Merging arrays that hold CSV data into one object array
- How do I access values in a d3 json file with nested array objects
- Merge two javascript objects if key is equal with either lodash, vanilla js, or d3
- Modify Pie Chart code to use with array of objects
- Manipulate array of objects to new array with average number.
- Selecting a specific value from an array of objects in javascript
- How to change JSON data to Javascript array of objects with D3
- Single stacked bar with array of objects
- How to return only unique value from array objects bound to an element in d3.js
- Charting a D3 line chart with an array of objects
- Transform a single object into an array of objects
More Query from same tag
- How to force nvd3 to display the equal number of ticks as that of the values plotted on the graph-nvd3
- How to avoid sluggish d3 force layout on Android?
- Beginner: Importing CSV to D3
- d3.js Update time domain based on brush selection
- Plot a graph using D3.JS
- D3 chart can't update -- enter and exit property of selection both empty
- Gap between bars C3.js
- D3 axis ticks and grid lines do not align
- how do you set a fixed link distance with d3 v4
- Programmatically setting d3 zoom transform
- d3: Avoid unchanged nodes?
- Insert text inside Circle in D3 chart
- Problem with label position with more than one nvd3 graph on one site
- Why is this tree layout giving me an infinite loop?
- DC js pie chart
- how to stop the slider as I hover over it?
- Dc.js: Bar-chart, bars are overlapping
- D3 network chart with arrow head pointing towards source node
- Render tick at zero y-value with d3 series plot
- Efficiently calculating convex hulls in D3 force-directed graph
- D3 JS - tick spacing on time axis gets messed up by month boundary
- Attempting to implement a svg:clipPath but end up with almost a blank screen
- d3 multi-line voronoi, trouble with showing data on mouseover
- D3: Example for hierarchical Matrix example
- How to use ternary conditional on object in loop?
- D3.js click to function using .on() method
- d3js interact with text inside a node using mouseover
- Placing the nodes in the center of screen in d3 force layout
- Make D3.js width and height 100%
- D3 axis label rotate transition not smooth