如何对 Angular 上的数组进行排序和映射 7
How to Sort and Map an Array on Angular 7
我有这个数组:
myArray = {
[0]{priority: 1, percent: 90, correct: true}
[1]{priority: 1, percent: 20, correct: true}
[2]{priority: 2, percent: 50, correct: true}
[3]{priority: 2, percent: 50, correct: true}
[4]{priority: 3, percent: 70, correct: true}
[5]{priority: 3, percent: 50, correct: true}
}
如何按优先级添加百分比,然后将所有给我超过 100 的结果的值“正确”更改为“错误”?
例如:
- 优先级 1 有 2 个值:90 和 20 = 110
- 优先级 2 有 2 个值:50 和 50 = 100
- 优先级 3 有 2 个值:70 和 50 = 120
- 因此所有优先级为 1 的对象都应将值“correct”更改为“false”
- 所有优先级为 2 的对象都应将值“correct”更改为“correct”
- 所有优先级为 3 的对象都应将值“correcto”更改为“false”
提供快速解决方案,但也可能有很多更好的解决方案:
myArray = [
{priority: 1, percent: 90, correct: true},
{priority: 1, percent: 20, correct: true},
{priority: 2, percent: 50, correct: true},
{priority: 2, percent: 50, correct: true},
{priority: 3, percent: 70, correct: true},
{priority: 3, percent: 50, correct: true},
];
tempMap = new Map();
myArray.forEach( (item) => {
if(tempMap.has(item.priority)) {
tempMap.set(item.priority, tempMap.get(item.priority) + item.percent);
}
else {
tempMap.set(item.priority, item.percent);
}
});
myArray.forEach( (item) => {
item.correct = tempMap.get(item.priority) > 100 ? false : true;
});
console.log(myArray);
const myArray: { priority: number; percent: number; correct: boolean }[] = [
{ priority: 1, percent: 90, correct: true },
{ priority: 1, percent: 20, correct: true },
{ priority: 2, percent: 50, correct: true },
{ priority: 2, percent: 50, correct: true },
{ priority: 3, percent: 70, correct: true },
{ priority: 3, percent: 50, correct: true },
];
const newArr = myArray
.sort((a, b) => a.priority - b.priority)
.map((val, i, myArray) => {
const percentSum = myArray
.filter((a) => a.priority == val.priority)
.map((b) => b.percent)
.reduce((a, b) => a + b, 0);
const arrObj = {
...val,
correct: percentSum > 100 ? false : true,
};
return arrObj;
});
console.log(newArr);
输出
[{
correct: false,
percent: 90,
priority: 1
}, {
correct: false,
percent: 20,
priority: 1
}, {
correct: true,
percent: 50,
priority: 2
}, {
correct: true,
percent: 50,
priority: 2
}, {
correct: false,
percent: 70,
priority: 3
}, {
correct: false,
percent: 50,
priority: 3
}]
我有这个数组:
myArray = {
[0]{priority: 1, percent: 90, correct: true}
[1]{priority: 1, percent: 20, correct: true}
[2]{priority: 2, percent: 50, correct: true}
[3]{priority: 2, percent: 50, correct: true}
[4]{priority: 3, percent: 70, correct: true}
[5]{priority: 3, percent: 50, correct: true}
}
如何按优先级添加百分比,然后将所有给我超过 100 的结果的值“正确”更改为“错误”?
例如:
- 优先级 1 有 2 个值:90 和 20 = 110
- 优先级 2 有 2 个值:50 和 50 = 100
- 优先级 3 有 2 个值:70 和 50 = 120
- 因此所有优先级为 1 的对象都应将值“correct”更改为“false”
- 所有优先级为 2 的对象都应将值“correct”更改为“correct”
- 所有优先级为 3 的对象都应将值“correcto”更改为“false”
提供快速解决方案,但也可能有很多更好的解决方案:
myArray = [
{priority: 1, percent: 90, correct: true},
{priority: 1, percent: 20, correct: true},
{priority: 2, percent: 50, correct: true},
{priority: 2, percent: 50, correct: true},
{priority: 3, percent: 70, correct: true},
{priority: 3, percent: 50, correct: true},
];
tempMap = new Map();
myArray.forEach( (item) => {
if(tempMap.has(item.priority)) {
tempMap.set(item.priority, tempMap.get(item.priority) + item.percent);
}
else {
tempMap.set(item.priority, item.percent);
}
});
myArray.forEach( (item) => {
item.correct = tempMap.get(item.priority) > 100 ? false : true;
});
console.log(myArray);
const myArray: { priority: number; percent: number; correct: boolean }[] = [
{ priority: 1, percent: 90, correct: true },
{ priority: 1, percent: 20, correct: true },
{ priority: 2, percent: 50, correct: true },
{ priority: 2, percent: 50, correct: true },
{ priority: 3, percent: 70, correct: true },
{ priority: 3, percent: 50, correct: true },
];
const newArr = myArray
.sort((a, b) => a.priority - b.priority)
.map((val, i, myArray) => {
const percentSum = myArray
.filter((a) => a.priority == val.priority)
.map((b) => b.percent)
.reduce((a, b) => a + b, 0);
const arrObj = {
...val,
correct: percentSum > 100 ? false : true,
};
return arrObj;
});
console.log(newArr);
输出
[{
correct: false,
percent: 90,
priority: 1
}, {
correct: false,
percent: 20,
priority: 1
}, {
correct: true,
percent: 50,
priority: 2
}, {
correct: true,
percent: 50,
priority: 2
}, {
correct: false,
percent: 70,
priority: 3
}, {
correct: false,
percent: 50,
priority: 3
}]