javascript - 计算数组中每 10 个对象的成功率
javascript - calculate success rate for every 10 objects in an array
我有以下数组,每个用户最多可以包含 30 个动态对象。
const array = [
{name: x, succeeded: true},
{name: y, succeeded: false},
{name: z, succeeded: true},
]
我需要做的是制作一个函数,该函数将检查每 10 个对象的成功率,并添加一个名为“successRate”的键,最后 10 个对象的成功率取决于成功的次数是否为真。如果剩下的对象少于 10 个,它将根据剩余对象的数量计算成功率(例如,如果数组中有 27 个对象,它将检查前 10 个对象的成功率,然后检查其他 10 个对象的成功率,然后检查 7 个对象的成功率)。
示例数组的输出应为:
const array = [
{name: x, succeeded: true, successRate: 66.66%},
{name: y, succeeded: false, successRate: 66.66%},
{name: z, succeeded: true, successRate: 66.66%},
]
然而,如果数组的长度是例如 13,它将首先检查前 10 个对象并为它们提供相同的成功率,然后检查其他 3 个并为它们提供相同的成功率。
例如:
const array = [
{name: 1, succeeded: true, successRate: 80%},
{name: 2, succeeded: true, successRate: 80%},
{name: 3, succeeded: true, successRate: 80%},
{name: 4, succeeded: true, successRate: 80%},
{name: 5, succeeded: false, successRate: 80%},
{name: 6, succeeded: true, successRate: 80%},
{name: 7, succeeded: true, successRate: 80%},
{name: 8, succeeded: false, successRate: 80%},
{name: 9, succeeded: true, successRate: 80%},
{name: 10, succeeded: true, successRate: 80%},
{name: 11, succeeded: false, successRate: 66.66%},
{name: 12, succeeded: true, successRate: 66.66%},
{name: 13, succeeded: true, successRate: 66.66%},
]
您可以执行以下操作:
const array = [
{name: 1, succeeded: true,},
{name: 2, succeeded: true},
{name: 3, succeeded: true},
{name: 4, succeeded: true},
{name: 5, succeeded: false},
{name: 6, succeeded: true},
{name: 7, succeeded: true},
{name: 8, succeeded: false},
{name: 9, succeeded: true,},
{name: 10, succeeded: true},
{name: 11, succeeded: false},
{name: 12, succeeded: true},
{name: 13, succeeded: true},
]
const arrays = [];
const size = 10;
// first split input into chunks of 10 or less
for (let i = 0; i < array.length; i += size) {
arrays.push(array.slice(i, i + size));
}
const result = arrays.forEach(chunk => { // do following for each chunk
//find number of succeeded items
let succeeded = chunk.filter(item => item.succeeded).length;
// find success rate based on number of items in chunk
let rate = `${(succeeded/chunk.length) * 100}%`;
// add current success rate to every item in chunk
chunk.forEach(item => item.successRate = rate);
});
console.log(arrays.flat());
这是一个使用递归对数组进行分块的解决方案...
const
array = [{ name: 1 }, { name: 2, succeeded: true }, { name: 3, succeeded: true }, { name: 4, succeeded: true }, { name: 5, succeeded: false }, { name: 6, succeeded: true }, { name: 7, succeeded: true }, { name: 8, succeeded: false }, { name: 9, succeeded: true }, { name: 10, succeeded: true }, { name: 11, succeeded: false }, { name: 12, succeeded: true }, { name: 13, succeeded: true },],
calculateSucessRate = (arr) => {
const { sum, length } = arr.reduce((a, { succeeded }) => {
if (succeeded !== undefined) {
a.length += 1;
a.sum += succeeded;
}
return a;
}, { sum: 0, length: 0 });
return sum / length;
},
successRateByChunk = (arr, chunkSize) => {
if (!arr.length) { return []; }
const chunk = arr.slice(0, chunkSize);
const successRate = `${calculateSucessRate(chunk) * 100}%`;
return [
...chunk.map(o => ({ ...o, successRate })),
...successRateByChunk(arr.slice(chunkSize), chunkSize)
];
};
console.log(successRateByChunk(array, 10));
.as-console-wrapper { max-height: 100% !important; top: 0; }
我有以下数组,每个用户最多可以包含 30 个动态对象。
const array = [
{name: x, succeeded: true},
{name: y, succeeded: false},
{name: z, succeeded: true},
]
我需要做的是制作一个函数,该函数将检查每 10 个对象的成功率,并添加一个名为“successRate”的键,最后 10 个对象的成功率取决于成功的次数是否为真。如果剩下的对象少于 10 个,它将根据剩余对象的数量计算成功率(例如,如果数组中有 27 个对象,它将检查前 10 个对象的成功率,然后检查其他 10 个对象的成功率,然后检查 7 个对象的成功率)。
示例数组的输出应为:
const array = [
{name: x, succeeded: true, successRate: 66.66%},
{name: y, succeeded: false, successRate: 66.66%},
{name: z, succeeded: true, successRate: 66.66%},
]
然而,如果数组的长度是例如 13,它将首先检查前 10 个对象并为它们提供相同的成功率,然后检查其他 3 个并为它们提供相同的成功率。 例如:
const array = [
{name: 1, succeeded: true, successRate: 80%},
{name: 2, succeeded: true, successRate: 80%},
{name: 3, succeeded: true, successRate: 80%},
{name: 4, succeeded: true, successRate: 80%},
{name: 5, succeeded: false, successRate: 80%},
{name: 6, succeeded: true, successRate: 80%},
{name: 7, succeeded: true, successRate: 80%},
{name: 8, succeeded: false, successRate: 80%},
{name: 9, succeeded: true, successRate: 80%},
{name: 10, succeeded: true, successRate: 80%},
{name: 11, succeeded: false, successRate: 66.66%},
{name: 12, succeeded: true, successRate: 66.66%},
{name: 13, succeeded: true, successRate: 66.66%},
]
您可以执行以下操作:
const array = [
{name: 1, succeeded: true,},
{name: 2, succeeded: true},
{name: 3, succeeded: true},
{name: 4, succeeded: true},
{name: 5, succeeded: false},
{name: 6, succeeded: true},
{name: 7, succeeded: true},
{name: 8, succeeded: false},
{name: 9, succeeded: true,},
{name: 10, succeeded: true},
{name: 11, succeeded: false},
{name: 12, succeeded: true},
{name: 13, succeeded: true},
]
const arrays = [];
const size = 10;
// first split input into chunks of 10 or less
for (let i = 0; i < array.length; i += size) {
arrays.push(array.slice(i, i + size));
}
const result = arrays.forEach(chunk => { // do following for each chunk
//find number of succeeded items
let succeeded = chunk.filter(item => item.succeeded).length;
// find success rate based on number of items in chunk
let rate = `${(succeeded/chunk.length) * 100}%`;
// add current success rate to every item in chunk
chunk.forEach(item => item.successRate = rate);
});
console.log(arrays.flat());
这是一个使用递归对数组进行分块的解决方案...
const
array = [{ name: 1 }, { name: 2, succeeded: true }, { name: 3, succeeded: true }, { name: 4, succeeded: true }, { name: 5, succeeded: false }, { name: 6, succeeded: true }, { name: 7, succeeded: true }, { name: 8, succeeded: false }, { name: 9, succeeded: true }, { name: 10, succeeded: true }, { name: 11, succeeded: false }, { name: 12, succeeded: true }, { name: 13, succeeded: true },],
calculateSucessRate = (arr) => {
const { sum, length } = arr.reduce((a, { succeeded }) => {
if (succeeded !== undefined) {
a.length += 1;
a.sum += succeeded;
}
return a;
}, { sum: 0, length: 0 });
return sum / length;
},
successRateByChunk = (arr, chunkSize) => {
if (!arr.length) { return []; }
const chunk = arr.slice(0, chunkSize);
const successRate = `${calculateSucessRate(chunk) * 100}%`;
return [
...chunk.map(o => ({ ...o, successRate })),
...successRateByChunk(arr.slice(chunkSize), chunkSize)
];
};
console.log(successRateByChunk(array, 10));
.as-console-wrapper { max-height: 100% !important; top: 0; }