this is my dataset
dfg=[[{"key":"Product_Description","value":"$100.83 Business Select Plan excl GST"},{"key":">20MB/30","value":1480},{"key":">200MB/30","value":1234},{"key":">2048MB/30","value":697},{"key":">5120MB/30","value":379},{"key":">10240MB/30","value":174}],[{"key":"Product_Description","value":"$119.95 Business Share Everything Plan"},{"key":">20MB/30","value":66},{"key":">200MB/30","value":57},{"key":">2048MB/30","value":30},{"key":">5120MB/30","value":14},{"key":">10240MB/30","value":6}]]
THis is my function:
function format_data_to_percent(d){
var data = d
data.map(function(a){
var sum = a.slice(1).map(function(a){ return a.value; }).reduce(function(a,b){return a*1+b*1},0);
//console.log("sum",sum);
a.slice(1).filter(function(a){
//console.log(a)
a.value=a.value/sum
//console.log(a)
});
return a;
})
//START TEST
//END TEST
return data
}
What I am trying to do is get the percentage value for example I first get the values in each element
dfg[0].slice(1).map(function(a){ return a.value; })
[1480, 1234, 697, 379, 174]
Then I use the reduce function to sum this array:
dfg[0].slice(1).map(function(a){ return a.value; }).reduce(function(a,b){return a*1+b*1},0);
3964
I then do a slice and filter on the values and the divide them by the sum
dfg[0].slice(1).filter(function(a){
console.log(a)
console.log(a.value)
console.log(sum)
a.value=a.value/sum
//console.log(a)
});
this then gives me dfg after manipulating the first element
[[{"key":"Product_Description","value":"$100.83 Business Select Plan excl GST"},
{"key":">20MB/30","value":0.37336024217961655},
{"key":">200MB/30","value":0.3113017154389506},
{"key":">2048MB/30","value":0.17583249243188698},
{"key":">5120MB/30","value":0.09561049445005046},
{"key":">10240MB/30","value":0.04389505549949546}],
[{"key":"Product_Description","value":"$119.95 Business Share Everything Plan"},
{"key":">20MB/30","value":66},
{"key":">200MB/30","value":57},
{"key":">2048MB/30","value":30},
{"key":">5120MB/30","value":14},
{"key":">10240MB/30","value":6}]]
This gives me the result that I want
> 1480/3964
0.37336024217961655
> 1234/3964
0.3113017154389506
> 697/3964
0.17583249243188698
> 379/3964
0.09561049445005046
> 174/3964
0.04389505549949546
>
What I am looking for is a way to test this for a bigger dataset ot make sure the results are as expected always.Can anyone advise?
I have tried something like this
//START TEST
// create a test case for it //amke this better
vals=data_p[0].slice(1).map(function(a){ return a.value; })
//["11", "33", "10", "55", "10"]
console.log("vals",vals)
sum = data_p[0].slice(1).map(function(a){ return a.value; }).reduce(function(a,b){return a*1+b*1},0);
//119
vals2=vals.map(function(a){ return a/sum})
//[0.09243697478991597, 0.2773109243697479, 0.08403361344537816, 0.46218487394957986, 0.08403361344537816]
console.log(data[0][1].value)
console.log(data[0][2].value)
console.log(data[0][3].value)
console.log(data[0][4].value)
console.log(data[0][5].value)
console.log(vals2[0])
console.log(vals2[1])
console.log(vals2[2])
console.log(vals2[3])
console.log(vals2[4])
console.log("test1\-\-> ",data[0][1].value == vals2[0])
console.log("test2\-\> ",data[0][2].value == vals2[1])
console.log("test3\-\> ",data[0][3].value == vals2[2])
console.log("test4\-\> ",data[0][4].value == vals2[3])
console.log("test5\-\> ",data[0][5].value == vals2[4])
//END TEST
This is the log from my console for m reference:
dfg
[[Object { key="Product_Description", value="$100.83 Business Select Plan excl GST"}, Object { key=">20MB/30", value=0.37336024217961655}, Object { key=">200MB/30", value=0.3113017154389506}, 3 more...], [Object { key="Product_Description", value="$119.95 Business Share Everything Plan"}, Object { key=">20MB/30", value=66}, Object { key=">200MB/30", value=57}, 3 more...]]
function pp(s) {return JSON.stringify(s);}
undefined
pp(dfg)
"[[{"key":"Product_Description","value":"$100.83 Business Select Plan excl GST"},{"key":">20MB/30","value":0.37336024217961655},{"key":">200MB/30","value":0.3113017154389506},{"key":">2048MB/30","value":0.17583249243188698},{"key":">5120MB/30","value":0.09561049445005046},{"key":">10240MB/30","value":0.04389505549949546}],[{"key":"Product_Description","value":"$119.95 Business Share Everything Plan"},{"key":">20MB/30","value":66},{"key":">200MB/30","value":57},{"key":">2048MB/30","value":30},{"key":">5120MB/30","value":14},{"key":">10240MB/30","value":6}]]"
Aucun commentaire:
Enregistrer un commentaire