我是JS的新手
我有一个数组和一个相同数组的对象。
我想将总数归为特定类别。
例如:
1.Critical: from 30-45
2.High: from 16-27
3.Medium: from 5-15
4.Low:from 3-4
数组布局我有两种格式。
通过浏览器:
slect_score
[{…}]0:
4: 1
5: 3
15: 6
20: 1
30: 2
45:2
__proto__: Objectlength: 1__proto__: Array(0)
从浏览器中:
score_map
{4: 1, 5: 3, 15: 6, 20: 1, 30: 2, 45: 2}
__proto__:Object
总而言之,我想对数字进行分组并获得每个类别的出现总数。
对于上面的示例:我应该有:
1.Critical: 4
2.High: 1
3.Medium: 9
4.Low: 1
这是我的代码:我正在尝试将其插入Django模板:
var score = document.getElementById("score").innerHTML;
var score_array = [score.split(",").map(Number)];
var score_array_a = score_array[score_array.length -1];
var score_array_b = score_array_a.slice(0, -1);
var score_sum = score_array_b.reduce(function(a, b){ return a +
b;}, 0);
var score_avg = parseFloat(score_sum / score_array_b.length);
var score_map = score_array_b.reduce(function(obj, b) {
obj[b] = ++obj[b] || 1;
return obj;
}, {});
var slect_score = new Array(score_map);
在我的Django模板中:
<p id=score>
{% for asset in self.get_children %}{% for finding in
asset.get_children.specific %}
{% with finding.Identification|mul:Reconize
as ID_REC %}{{ID_REC|mul:Impact}},{% endwith %}{%
endfor %}{% endfor %}
</p>
Django元素主要用于计算,并获取每页及其计算的数量
THE ANSWER The Code of Mr. Bergur. And The way I made it to get the total number of each category.
var score = document.getElementById("vul_score").innerHTML;
var score_array = [score.split(",").map(Number)];
var score_array_a = score_array[score_array.length -1];
var score_array_b = score_array_a.slice(0, -1);
var score_sum = score_array_b.reduce(function(a, b){ return a +
b;}, 0);
var score_avg = parseFloat(score_sum / score_array_b.length);
var score_map = score_array_b.reduce(function(obj, b) {
obj[b] = ++obj[b] || 1;
return obj;
}, {})
var scoore = Object.entries(score_map).map(e => Object.assign(e, {
0: +e[0] }));
const scores = [{
name: 'critical',
low: 30,
high: 45,
count: 0
},{
name: 'high',
low: 16,
high: 27,
count: 0
},{
name: 'medium',
low: 5,
high: 15,
count: 0
},{
name: 'low',
low: 3,
high: 4,
count: 0
}]
const score_map_keys = Object.keys(score_map);
scores.forEach(s => {
score_map_keys.forEach(key => {
let count = score_map[key];
if (key >= s.low && key <= s.high) {
s.count = s.count+count
}
});
})
要获得我所做的每个类别的总数:
[scores[0].count, scores[1].count, scores[2].count, scores[3].count]
我真的要感谢贝尔格先生。
答案 0 :(得分:0)
有几种方法可以做到这一点。这是一种方法。
1-您拥有计分系统,但是您需要以一种代码可以读取它的方式来安排它。
一种方法是:
const scores = [{
name: 'critical',
low: 30,
high: 45,
count: 0
},{
name: 'high',
low: 16,
high: 27,
count: 0
},{
name: 'medium',
low: 5,
high: 15,
count: 0
},{
name: 'low',
low: 3,
high: 4,
count: 0
}]
这里有一个对象数组。每个对象都有4个属性:名称,最高允许值,最低允许值和当前计数。我们将在稍后的代码中对其进行修改。
2-我们可以使用.forEach
遍历该数组scores.forEach(scoreObject => {
console.log('This is '+ scoreObject.name)
console.log('Highest value is '+ scoreObject.high)
console.log('Lowest value is '+ scoreObject.low)
})
3-您也拥有每个乐谱的计数,但是您必须能够循环遍历以将所有乐谱相加。这是一个javascript对象,循环浏览javascript对象的最佳方法是使用Object.keys。
const score_map = {
4: 1,
5: 3,
15: 6,
20: 1,
30: 2,
45: 2
}
const score_map_keys = Object.keys(score_map);
4-我们可以遍历所有分数。
score_map_keys.forEach(key => {
let count = score_map[key];
console.log('Key ' + key + ' has value ' + count);
});
5-现在我们可以将它们放在一起。对于每个分数,我们都需要仔细核算并加总。
scores.forEach(scoreObject => {
console.log('Looking at ' + scoreObject.name);
score_map_keys.forEach(key => {
let count = score_map[key];
if (key >= scoreObject.low && key <= scoreObject.high) {
console.log(key + ' matches the allowed range');
scoreObject.count = scoreObject.count+count
}
});
})
console.log(scores);
您可以在以下jsfiddle中找到有效的代码:https://jsfiddle.net/j6734vtq/24/
打开控制台(ctr + shift + i),然后按运行。