javascript - JS 在对象数组中查找匹配项,如果其值为 0,则将其删除,否则将其移动到数组的末尾
问题描述
数据集:
var labels = ["Caterer", "INDEED", "Sarova Website", "Send to Friend", "alexandre-creed.com", "amdas.co.uk", "catabaseuk.co.uk", "cerecruit.co.uk", "el8recruitment.com", "equinoxresourcing.co.uk", "gatewayjobs.co.uk", "hotelstaffpec.co.uk", "jwrecruitment.co.uk", "marshallhr.co.uk", "marshallhr.com (Sarova)", "momentumrecruitment.com", "peoplebank.com", "ph-recruitment.co.uk", "platinum-hospitality.co.uk", "q", "talenthive.co.uk", "towngate-personnel.co.uk"];
var dataSet = {
"ADVERTS_PUBLISHED": ["102", "130", "153", "2", "2", "2", "2", "4", "2", "5", "2", "4", "3", "8", "4", "4", "4", "1", "3", "4", "1", "2"],
"VIEWS": ["1642", "10566", "45269", "7", "4", "1", "11", "9", "11", "12", "5", "0", "14", "6", "3", "13", "19", "2", "8", "3", "2", "2"],
"CLICKS": ["1992", "3628", "4458", "4", "2", "2", "7", "3", "11", "5", "6", "9", "10", "15", "10", "4", "34", "10", "10", "3", "0", "8"],
"SUBMITTED": ["1101", "877", "1290", "2", "3", "2", "10", "14", "11", "5", "5", "7", "6", "14", "8", "6", "17", "7", "9", "4", "1", "6"],
"PENDING": ["115", "26", "93", "0", "1", "0", "7", "3", "6", "2", "0", "0", "4", "8", "0", "3", "0", "0", "1", "0", "1", "0"],
"FILTERED": ["546", "493", "764", "1", "2", "2", "9", "12", "10", "5", "4", "5", "5", "12", "2", "3", "4", "6", "4", "0", "1", "5"],
"SHORTLISTED": ["37", "23", "32", "1", "0", "0", "0", "1", "0", "2", "0", "1", "0", "5", "0", "0", "0", "0", "2", "0", "0", "1"],
"REGRETTED": ["103", "28", "52", "0", "1", "0", "1", "6", "5", "3", "0", "2", "1", "1", "0", "0", "1", "3", "1", "0", "0", "2"],
"INTERVIEWED": ["62", "45", "88", "0", "0", "0", "1", "2", "2", "2", "0", "3", "0", "3", "0", "2", "0", "4", "0", "0", "0", "1"],
"OFFERED": ["4", "10", "20", "0", "0", "0", "0", "0", "0", "0", "0", "3", "0", "2", "0", "0", "0", "0", "0", "0", "0", "0"],
"OFFERED_AND_DECLINED": ["0", "0", "5", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"],
"REGRETTED_AND_COMM": ["587", "334", "533", "0", "0", "2", "1", "1", "2", "1", "0", "1", "2", "4", "0", "1", "0", "0", "0", "2", "0", "0"],
"ACTUAL_HIRED": ["4", "2", "8", "0", "0", "0", "0", "0", "0", "0", "0", "2", "0", "1", "0", "0", "0", "0", "0", "0", "0", "0"]
};
以下代码计算一个名为 slices 的数组,该数组包含要包含在图表中的所有切片的属性标签和值的对象。所有低于 1% 的值都由标有“其他”的对象求和并保存。
如果 Others 值为 0,我将如何使“Others”始终成为 slices 数组中的最后一个元素并完全删除它?
//create dataset that includes the others grouping
dataSet2 = [];
Object.keys(dataSet).forEach(function (item) {
dataSet2[item] = groupOthers(dataSet[item], 1);
});
//groups data points together
function groupOthers(dataSet, thresholdPercent = 10) {
const values = dataSet.map(v => Number(v));
const valueSum = values.reduce((a, b) => a + b, 0);
const slices = values.map((v, i) => ({ label: labels[i], value: v }))
.reduce((accumulator, currObj) => {
const percent = 100 * currObj.value / valueSum;
if (percent < thresholdPercent) {
const others = accumulator.find(o => o.label == 'Others');
if (!others) {
return accumulator.concat({ label: 'Others', value: currObj.value });
}
others.value += currObj.value;
} else {
accumulator.push(currObj);
}
return accumulator;
}, []);
return slices;
}
我曾尝试像这样更改代码,但我绝不是 JS 专家:
//find Others object in array and move it to the last element if it exists
obj = slices.find(o => o.label === 'Others');
if(obj !== null){
//move others to last element of array
slices.push(slices.shift());
//get last element of array
var last_element = slices[slices.length - 1];
if(last_element.label == 'Others'){
//if value of others is 0 remove last element from array
if(last_element.value == 0){
slices.splice(-1,1)
}
}
}
return slices;
输出将类似于以下内容
0: {label: "whatever", value: 4}
1: {label: "...", value: 2}
2: {label: "....", value: 1}
3: {label: "....", value: 1}
4: {label: "....", value: 2}
5: {label: "....", value: 1}
6: {label: "....", value: 1}
7: {label: "....", value: 1}
8: {label: "....", value: 1}
9: {label: "Others", value: 2}
解决方案
您可以使用Object.entries
先切换到数组格式,进行映射和过滤,然后使用相反Object.fromEntries
的方式取回对象表示:
var labels = ["Caterer", "INDEED", "Sarova Website", "Send to Friend", "alexandre-creed.com", "amdas.co.uk", "catabaseuk.co.uk", "cerecruit.co.uk", "el8recruitment.com", "equinoxresourcing.co.uk", "gatewayjobs.co.uk", "hotelstaffpec.co.uk", "jwrecruitment.co.uk", "marshallhr.co.uk", "marshallhr.com (Sarova)", "momentumrecruitment.com", "peoplebank.com", "ph-recruitment.co.uk", "platinum-hospitality.co.uk", "q", "talenthive.co.uk", "towngate-personnel.co.uk"];
var dataSet = {"ADVERTS_PUBLISHED": ["102", "130", "153", "2", "2", "2", "2", "4", "2", "5", "2", "4", "3", "8", "4", "4", "4", "1", "3", "4", "1", "2"],"VIEWS": ["1642", "10566", "45269", "7", "4", "1", "11", "9", "11", "12", "5", "0", "14", "6", "3", "13", "19", "2", "8", "3", "2", "2"],"CLICKS": ["1992", "3628", "4458", "4", "2", "2", "7", "3", "11", "5", "6", "9", "10", "15", "10", "4", "34", "10", "10", "3", "0", "8"],"SUBMITTED": ["1101", "877", "1290", "2", "3", "2", "10", "14", "11", "5", "5", "7", "6", "14", "8", "6", "17", "7", "9", "4", "1", "6"],"PENDING": ["115", "26", "93", "0", "1", "0", "7", "3", "6", "2", "0", "0", "4", "8", "0", "3", "0", "0", "1", "0", "1", "0"],"FILTERED": ["546", "493", "764", "1", "2", "2", "9", "12", "10", "5", "4", "5", "5", "12", "2", "3", "4", "6", "4", "0", "1", "5"],"SHORTLISTED": ["37", "23", "32", "1", "0", "0", "0", "1", "0", "2", "0", "1", "0", "5", "0", "0", "0", "0", "2", "0", "0", "1"],"REGRETTED": ["103", "28", "52", "0", "1", "0", "1", "6", "5", "3", "0", "2", "1", "1", "0", "0", "1", "3", "1", "0", "0", "2"],"INTERVIEWED": ["62", "45", "88", "0", "0", "0", "1", "2", "2", "2", "0", "3", "0", "3", "0", "2", "0", "4", "0", "0", "0", "1"],"OFFERED": ["4", "10", "20", "0", "0", "0", "0", "0", "0", "0", "0", "3", "0", "2", "0", "0", "0", "0", "0", "0", "0", "0"],"OFFERED_AND_DECLINED": ["0", "0", "5", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"],"REGRETTED_AND_COMM": ["587", "334", "533", "0", "0", "2", "1", "1", "2", "1", "0", "1", "2", "4", "0", "1", "0", "0", "0", "2", "0", "0"],"ACTUAL_HIRED": ["4", "2", "8", "0", "0", "0", "0", "0", "0", "0", "0", "2", "0", "1", "0", "0", "0", "0", "0", "0", "0", "0"]};
var threshold = 10;
var res = Object.fromEntries(Object.entries(dataSet).map(([metric, values]) =>
[metric, values
.filter(v => +v > threshold)
.map((v, i) => ({label: labels[i], value: +v}))
.sort((a, b) => b.value - a.value)
.concat({
label: "others",
value: values.reduce((acc, v) => acc + (+v <= threshold && +v), 0)
})
]
));
console.log(res);
推荐阅读
- javascript - Vue-multiselect如何进行两个输入
- r - Spread data based on multiple key variables
- javascript - Bootstrap: $('#myModal').modal('show'); causes screen to go black
- python - How to mirror value in a column in a new column based on ID
- r - Replace multiple strings comprising of a different number of characters with one gsubfn()
- bigdata - Comparison between Big Data and Data Lakes , difference and similarities
- android - Google Pay AutoResolveHelper.resolveTask() 未在 Fragment 中调用 onActivityResult
- spring - spring rest docs - 折叠长 jsons
- ruby - 权限错误:在使用 ruby sdk 创建 facebook 自定义受众时连接到 Business Manager 以创建此受众
- kotlin - What does a Coroutine Join do?