如何在一个对象数组中使用相同的键汇总值?

发布于 2025-01-22 10:46:41 字数 2422 浏览 4 评论 0原文

假设我有类似的对象:

this.arrayOfObjects = [{
    {
        "grouppresentation": "11",
        "evaluation": null,
        "datacontext": null,
        "category": "Group Presentation"
    },
    {
        "grouppresentation": null,
        "evaluation": "23",
        "datacontext": null,
        "category": "Evaluation"
    },
    {
        "grouppresentation": null,
        "evaluation": "46",
        "datacontext": null,
        "category": "Evaluation"
    },
    {
        "grouppresentation": "44",
        "evaluation": null,
        "datacontext": null,
        "category": "Group Presentation"
    },
    {
        "grouppresentation": null,
        "evaluation": null,
        "datacontext": "21",
        "category": "Data Context"
    }
}]

每个对象中的键由“类别”键中的值定义。这意味着如果“类别” =“组呈现”,则存在一个称为“ grouppersentation”的密钥,其中使用replace('',','').tolowercase()将“类别”值转换为“类别”值。

它没有如上显示,但是有一个带有“类别” =“数据上下文”的对象,因此在同一对象中有一个称为“ dataContext”的键。

另一个重要说明是,如果“类别” =“组呈现”,则“ grouppersentation”键将具有一个值,即一个数字。其他键将设置为空,因此具有实际值的唯一键是“类别”和“ grouppersentation”。像这样,但使用类别“评估”:

    {
        "grouppresentation": null,
        "evaluation": "46",
        "datacontext": null,
        "category": "Evaluation"
    },

目标

我想要的每个项目都具有相同的“类别”值,以总结代表该值的密钥值:

因此,

this.newArrayOfObjects = [{
    {
        "grouppresentation": "55",     -> because 11 + 44 from the values above
        "evaluation": null,
        "datacontext": null,
        "category": "Group Presentation"
    },
    {
        "grouppresentation": null,
        "evaluation": "69",    -> because 46 + 23 from the values above
        "datacontext": null,
        "category": "Evaluation"
    },
    {
        "grouppresentation": null,
        "evaluation": null,
        "datacontext": "21",
        "category": "Data Context"
    }
}]

到目前为止,我已经尝试了几种方法,其中之一是:

var newObject = {};

this.arrayOfObjects.forEach(function(item) {
  if (newObject.hasOwnProperty(item.category)) {
    newObject[item.name] = newObject[item.category.replace(' ', '').toLowerCase()] + item.category.replace(' ', '').toLowerCase();
  } else {
    newObject[item.name] = item.category.replace(' ', '').toLowerCase();
  }
});

但是,不幸的是,它不会到达任何地方并产生以下内容:

{undefined: 'grouppresentation'}

您是否知道如何实现上述方法?

Let's say I have an array of objects like so:

this.arrayOfObjects = [{
    {
        "grouppresentation": "11",
        "evaluation": null,
        "datacontext": null,
        "category": "Group Presentation"
    },
    {
        "grouppresentation": null,
        "evaluation": "23",
        "datacontext": null,
        "category": "Evaluation"
    },
    {
        "grouppresentation": null,
        "evaluation": "46",
        "datacontext": null,
        "category": "Evaluation"
    },
    {
        "grouppresentation": "44",
        "evaluation": null,
        "datacontext": null,
        "category": "Group Presentation"
    },
    {
        "grouppresentation": null,
        "evaluation": null,
        "datacontext": "21",
        "category": "Data Context"
    }
}]

The keys in each object is defined by the values in the "category" key. Meaning that if "category" = "Group Presentation", then there exists a key called "grouppresentation" where the "category" value has been converted using replace(' ', '').toLowerCase().

It is not shown above, but there's an object with "category" = "Data Context" and so there's a key in the same object called "datacontext".

Another important note is that if "category" = "Group Presentation", then the "grouppresentation" key will have a value i.e. a number. The other keys will be set to null, so the only keys with an actual value is "category" and "grouppresentation". Like so but with category "Evaluation":

    {
        "grouppresentation": null,
        "evaluation": "46",
        "datacontext": null,
        "category": "Evaluation"
    },

GOAL

I want for each item with the same 'category' value, to sum the values of the key that represents that value:

So,

this.newArrayOfObjects = [{
    {
        "grouppresentation": "55",     -> because 11 + 44 from the values above
        "evaluation": null,
        "datacontext": null,
        "category": "Group Presentation"
    },
    {
        "grouppresentation": null,
        "evaluation": "69",    -> because 46 + 23 from the values above
        "datacontext": null,
        "category": "Evaluation"
    },
    {
        "grouppresentation": null,
        "evaluation": null,
        "datacontext": "21",
        "category": "Data Context"
    }
}]

So far, I've attempted several methods, one of which being:

var newObject = {};

this.arrayOfObjects.forEach(function(item) {
  if (newObject.hasOwnProperty(item.category)) {
    newObject[item.name] = newObject[item.category.replace(' ', '').toLowerCase()] + item.category.replace(' ', '').toLowerCase();
  } else {
    newObject[item.name] = item.category.replace(' ', '').toLowerCase();
  }
});

However, it unfortunately does no get anywhere and yields the following:

{undefined: 'grouppresentation'}

Would you have any idea how achieve the above?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枫林﹌晚霞¤ 2025-01-29 10:46:41

到达那里。基本上,我们只检查每个迭代的类别中的匹配项。如果有匹配,我们将适当的值添加在一起(将两个转换为飞行的数字)

let arrayOfObjects = [{
    "grouppresentation": "11",
    "evaluation": null,
    "datacontext": null,
    "category": "Group Presentation"
  },
  {
    "grouppresentation": null,
    "evaluation": "23",
    "datacontext": null,
    "category": "Evaluation"
  },
  {
    "grouppresentation": null,
    "evaluation": "46",
    "datacontext": null,
    "category": "Evaluation"
  },
  {
    "grouppresentation": "44",
    "evaluation": null,
    "datacontext": null,
    "category": "Group Presentation"
  },
  {
    "grouppresentation": null,
    "evaluation": null,
    "datacontext": "21",
    "category": "Data Context"
  }
]

let newArrayOfObjects = arrayOfObjects.reduce((b, a) => {
  let ind = b.findIndex(e => e.category === a.category);
  let c = a.category.toLowerCase().split(' ').join('');
  if (ind > -1) {
    b[ind][c] = +b[ind][c] + +a[c]
  } else {
    a[c] = +a[c] || 0
    b.push(a)
  }
  return b;
}, []);

console.log(newArrayOfObjects)

Array#reduce is one way of getting there. Basically we just check each iteration for a match on the category. If there is match, we add the appropriate values together (converting both to numbers on the fly)

let arrayOfObjects = [{
    "grouppresentation": "11",
    "evaluation": null,
    "datacontext": null,
    "category": "Group Presentation"
  },
  {
    "grouppresentation": null,
    "evaluation": "23",
    "datacontext": null,
    "category": "Evaluation"
  },
  {
    "grouppresentation": null,
    "evaluation": "46",
    "datacontext": null,
    "category": "Evaluation"
  },
  {
    "grouppresentation": "44",
    "evaluation": null,
    "datacontext": null,
    "category": "Group Presentation"
  },
  {
    "grouppresentation": null,
    "evaluation": null,
    "datacontext": "21",
    "category": "Data Context"
  }
]

let newArrayOfObjects = arrayOfObjects.reduce((b, a) => {
  let ind = b.findIndex(e => e.category === a.category);
  let c = a.category.toLowerCase().split(' ').join('');
  if (ind > -1) {
    b[ind][c] = +b[ind][c] + +a[c]
  } else {
    a[c] = +a[c] || 0
    b.push(a)
  }
  return b;
}, []);

console.log(newArrayOfObjects)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文