如何遍历JSON数据数组。您可以提供代码帮助吗?
var percent=(100 * this.contributed) / this.max;
percent= Math.floor(percent);
这是逻辑。我需要将其应用于所有用于每个对象的对象
var dashboardval= [
{"contributed": 20, "max": 35 },
{"contributed": 22, "max": 35},
{'contributed': 35, "max": 35,},
{"contributed": 32, "max": 35}
];
答案 0 :(得分:0)
使用ES6,您可以通过减少对象的数组来做到这一点:
var objects = [{"contributed": 20, "max": 35}, {"contributed": 22, "max": 35}, {'contributed': 35, "max": 35}, {"contributed": 32, "max": 35}];
var percent = objects.reduce((acc, {contributed, max}) => ~~((100 * contributed) / max));
console.log(percent);