score:3
Accepted answer
You can use some maps and reduces to convert this in one line:
var quantities = [{"type": "apple","number": 51},
{"type": "orange","number": 19},
{"type": "lemon","number": 11}];
var joined = quantities.map(function(x) {
return Array.apply(null, new Array(x.number)).map(function (y) {
return x.type; }); }).reduce(function(prev, z) {
return prev.concat(z); }, [])
console.log(joined);
In case you were wondering what is Array.apply(null, new Array(x.number))
… this is an idiom to create an initialized array from an uninitialized array of specified length, for the reason that the former is mappable and the latter is not.
Source: stackoverflow.com
Related Query
- Create dataset from array of numbers with d3.js
- How to create multiple pie charts with D3 from a single array with multiple objects?
- Create an array from csv for use with D3.js library
- Javascript: Array of dictionaries, get all key value pairs from one dictionairy with condition
- How to get projected path definition strings (not SVG elements) from a GeoJSON dataset with D3?
- Create a JSON file from a Flask view to be used with D3
- How to summarize an array with group and rollup from d3-array?
- how to get data with tsv or csv to array in d3.js from a txt file?
- Calculate max from array with some non-numeric values
- D3 create buttons from an array of string containing names
- Create map that combines data from two files with common column - but data doesn't match one to one
- Given a range of date intervals, create an array of all 1440 minutes in a day with the frequency each minute appears in the source intervals
- d3js v5: Create a SVG with groups from a JSON file
- Enter() statement with a function from one data array to multiple arrays? (Adding multiple Gradient Paths)
- D3.js moving from tsv to json with nested array
- d3.js Array with categories and numbers
- how to create separate bar chart with JSON array set?
- Get max value from array with multiple columns
- Adding tooltip to SVG elements with text from an array
- How to create an array of nested objects from a csv file in D3.js?
- d3 create SVG path from array
- Creating array from csv with d3.group without group headers
- Create a table structure in d3js from array of array and draw text or graph
- How to create a heatmap with numbers using Leaflet map?
- D3.js - Create multi-ring donut chart dataset from JSON
- Group a HTML tabulation from multidimensional array with data driven documents
- Add new row dynamically to an array with headers created from a csv file
- D3.js visual selecting image URL from object array dataset
- How to create a new array using the data from the old one?
- translation using d3 with translation using data from an array in d3.js
More Query from same tag
- Rotate Donut Chart to Middle of Selected Slice
- d3 x axis custom values
- D3js v5 Trying to select multiple bars in bar chart using brush and saving values to variable and table
- AngularJS integration with D3.js
- Drag flickers on force-directed graph
- Avoid ovrelapping of points in technology radar
- d3.js zoom xScale / xAxis
- Resize centering force / change center - d3js v4
- d3 stacked bar chart issue
- D3 - data format required for cal-heatmap calendar heatmap?
- Drawing links that do not affect the physics of a force directed graph
- Bar Chart with Counts in dc.js
- Append rect after closing tag
- d3.rgb().darker/brighter
- Grab properties of the HTML __data__ property using jquery
- NVD3 Multiple Axis Barchart Overlapping Bars on Drawing
- D3.js Cannot read property 'length' of undefined
- d3 v4 TypeScript DefinitelyTyped Angular2 line with ScaleTime on the X axis
- D3.js - Error : d3.line() is not a function (changed from version 4 to 3)
- csv data to nested json tree in d3
- Filter paths and append text
- Rendering hexagons in d3.js in the wordmap
- d3.js Common Trait Chart
- Adding a label and path to a chart D3
- D3.js chart pie rotate with label
- why my two graph of d3.js is not combined in the single page?
- d3.js arrow directed graph: adding nodes/links on the fly
- d3 update old scale when pan and zoom
- Having trouble with D3v4 update pattern with 3 levels of nested data
- how to improve the drilling performance of d3 sunburst chart when there are too many paths? like dynamically show partial levels?