d3.js - Convert Javascript object to array of numbers -
i have object has set of numbers
> var size = d3.values(data[0]); >size output: ["0", "14.71", "50.0", "35.29"] >typeof(size); output: "object"
i want sum width , output below:
[0, 14.71, 64.71, 100]
but output got below:
>var width = [size[0], size[0]+size[1], size[0]+size[1]+size[2], size[0]+size[1]+size[2]+size[3]]; >width output: ["0", "014.71", "014.7150.0", "014.7150.035.29"]
please me fix this. in advance...
you use array#map
, thisargs , implicit casting number element.
var data = ["0", "14.71", "50.0", "35.29"], width = data.map(function (a) { return this.count += +a; }, { count: 0}); console.log(width);
es6 without use of thisarg (this
not work arrow function), closure of count
.
var data = ["0", "14.71", "50.0", "35.29"], width = data.map((count => => count += +a)(0)); console.log(width);
Comments
Post a Comment