如何从对象长度数组中删除子元素并合并元素数组属性



我有一个元素包含数组属性的对象数组。我需要根据标签值将数组减少到更短的数组,并合并并替换_data数组为如下所示的新数组:

const originalArray = [
{  label: "approved", data: [1, 0, 0, 0, 0, 0, 0] },
{  label: "approved", data: [0, 2, 0, 0, 0, 0, 0] },
{  label: "rejected", data: [0, 0, 3, 0, 0, 0, 0] },
{  label: "rejected", data: [0, 0, 0, 1, 0, 0, 0] },
{  label: "pending", data: [0, 1, 0, 0, 0, 0, 0] },
{  label: "pending", data: [0, 0, 0, 0, 0, 7, 0] }
];
const expectedArray = [
{  label: "approved", data: [1, 2, 0, 0, 0, 0, 0] },
{  label: "rejected", data: [0, 0, 3, 1, 0, 0, 0] },
{  label: "pending", data: [0, 1, 0, 0, 0, 7, 0] },

]

我尝试通过标签合并每个元素的数据属性,然后从它创建一个唯一的数组

const originalArray = [
{ label: "approved", data: [1, 0, 0, 0, 0, 0, 0] },
{ label: "approved", data: [0, 2, 0, 0, 0, 0, 0] },
{ label: "rejected", data: [0, 0, 3, 0, 0, 0, 0] },
{ label: "rejected", data: [0, 0, 0, 1, 0, 0, 0] },
{ label: "pending", data: [0, 1, 0, 0, 0, 0, 0] },
{ label: "pending", data: [0, 0, 0, 0, 0, 7, 0] }
];
const expectedArray = Object.values(originalArray.reduce((acc, curr) => {
if (acc[curr.label]) {
acc[curr.label].data = acc[curr.label].data.map((val, i) => val + curr.data[i]);
} else {
acc[curr.label] = {
label: curr.label,
data: [...curr.data]
};
}
return acc;
}, {}));
console.log(expectedArray);

使用Array.prototype.reduce()累加一个对象,其中每个键对label唯一,对于每个循环,如果label已经存在于累加器对象中,则添加一个包含累加器数据(对应值)的数据数组。如果带有该标签的对象不存在,则以相应的标签作为键创建一个新对象。

然后使用Object.values()从累积对象中获取值数组。

您可以使用reduce来完成此操作,但它可能稍微更直接,只是将每个标签的值累积到一个对象中,然后将其解包为所需的数组形式:

const originalArray = [
{  label: "approved", data: [1, 0, 0, 0, 0, 0, 0] },
{  label: "approved", data: [0, 2, 0, 0, 0, 0, 0] },
{  label: "rejected", data: [0, 0, 3, 0, 0, 0, 0] },
{  label: "rejected", data: [0, 0, 0, 1, 0, 0, 0] },
{  label: "pending", data: [0, 1, 0, 0, 0, 0, 0] },
{  label: "pending", data: [0, 0, 0, 0, 0, 7, 0] }
];
function combine(input) {
// Helper function to add two arrays element-by-element
const addArrays = (a, b) => a ? a.map((_,i) => a[i] + b[i]) : b;
// Accumulate arrays into an object keyed by label
const perLabel = {};
for (const { label, data } of input)
perLabel[label] = addArrays(perLabel[label], data);
// Convert the entries in the object into the expected array shape
return Object.entries(perLabel).map(([label, data]) => ({ label, data }));
}
const combined = combine(originalArray);
console.log(combined);

可以先按标签将数组聚合成一个map,然后再合并它们。

const originalArray = [
{ label: 'approved', data: [1, 0, 0, 0, 0, 0, 0] },
{ label: 'approved', data: [0, 2, 0, 0, 0, 0, 0] },
{ label: 'rejected', data: [0, 0, 3, 0, 0, 0, 0] },
{ label: 'rejected', data: [0, 0, 0, 1, 0, 0, 0] },
{ label: 'pending', data: [0, 1, 0, 0, 0, 0, 0] },
{ label: 'pending', data: [0, 0, 0, 0, 0, 7, 0] },
];
const map = new Map();
for (const o of originalArray) {
const dataArrs = map.get(o.label) || [];
map.set(o.label, [...dataArrs, o.data]);
}
const result = [];
for (const [label, dataArrs] of map.entries()) {
const data = dataArrs[0];
for (let i = 1; i < dataArrs.length; i++) {
for (let j = 0; j < dataArrs[i].length; j++) {
data[j] += dataArrs[i][j];
}
}
result.push({ label, data });
}
console.log(
'result:',
JSON.stringify(result, (k, v) => (k === 'data' ? v.toString() : v), 2)
);
div.as-console-wrapper {
max-height: 200px;
}

相关内容

  • 没有找到相关文章

最新更新