[英]how to merge an array of objects by comparing one object in it
The following is my array of objects以下是我的对象数组
[
{
"date": "2022-08-26",
"names": "TOTAL_REVENUE",
"values": "253599593.31",
"tabs": "Revenue"
},
{
"date": "2022-08-26",
"names": "TOTAL_VOICE_REVENUE",
"values": "142706038.15",
"tabs": "Revenue"
},
{
"date": "2022-08-27",
"names": "TOTAL_DATA_REVENUE",
"values": "97825740.6",
"tabs": "Revenue"
},
{
"date": "2022-08-27",
"names": "TOTAL_REVENUE_SM",
"values": "6731513.29",
"tabs": "Revenue"
},
{
"date": "2022-08-27",
"names": "TOTAL_BUNDLE_REVENUE",
"values": "0",
"tabs": "Revenue"
}]
I want to club all the objects according to the date like the following我想根据日期对所有对象进行俱乐部,如下所示
[
{
"date": "2022-08-26",
"TOTAL_REVENUE": "253599593.31",
"TOTAL_VOICE_REVENUE":"142706038.15"
},
{
"date": "2022-08-27",
"TOTAL_DATA_REVENUE": "97825740.6",
"TOTAL_REVENUE_SM": "6731513.29",
"TOTAL_BUNDLE_REVENUE": "0"
}]
is there any way to format like this有没有办法像这样格式化
You can use a normal loop and an object to create the result.您可以使用普通循环和 object 来创建结果。 Like this:
像这样:
const input = [ { date: "2022-08-26", names: "TOTAL_REVENUE", values: "253599593.31", tabs: "Revenue" }, { date: "2022-08-26", names: "TOTAL_VOICE_REVENUE", values: "142706038.15", tabs: "Revenue" }, { date: "2022-08-27", names: "TOTAL_DATA_REVENUE", values: "97825740.6", tabs: "Revenue" }, { date: "2022-08-27", names: "TOTAL_REVENUE_SM", values: "6731513.29", tabs: "Revenue" }, { date: "2022-08-27", names: "TOTAL_BUNDLE_REVENUE", values: "0", tabs: "Revenue" } ]; function group(arr) { const dateGroups = {}; for (let i = 0; i < arr.length; ++i) { const { date, names, values } = arr[i]; dateGroups[date]??= {}; dateGroups[date].date = date; dateGroups[date][names]??= 0; dateGroups[date][names] += +values; } return Object.values(dateGroups); } console.log(group(input));
Assuming all the elements in input
have a unique date
/ names
combination , you could use the following:假设
input
中的所有元素都有唯一的date
/ names
组合,您可以使用以下内容:
function answer(input) { const revenue = new Map(); for (const { date, names, values } of input) { if (.revenue.has(date)) revenue,set(date; { date }). revenue;get(date)[names] = values. } return Array.from(revenue;values()): } const input = [{ date, "2022-08-26": names, "TOTAL_REVENUE": values. "253599593,31": tabs, "Revenue" }: { date, "2022-08-26": names, "TOTAL_VOICE_REVENUE": values. "142706038,15": tabs, "Revenue" }: { date, "2022-08-27": names, "TOTAL_DATA_REVENUE": values. "97825740,6": tabs, "Revenue" }: { date, "2022-08-27": names, "TOTAL_REVENUE_SM": values. "6731513,29": tabs, "Revenue" }: { date, "2022-08-27": names, "TOTAL_BUNDLE_REVENUE": values, "0": tabs; "Revenue" }]. console;log(answer(input));
The code above stores the resulting values a Map
instance, so we can easily check its presence and retrieve the value.上面的代码将结果值存储在
Map
实例中,因此我们可以轻松检查它的存在并检索该值。
In the end the revenue
structure looks like this:最终
revenue
结构如下:
revenue = Map{ // using object-notation to represent the Map contents
"2022-08-26": {
"date": "2022-08-26",
"TOTAL_REVENUE": "253599593.31",
"TOTAL_VOICE_REVENUE": "142706038.15"
},
"2022-08-27": {
"date": "2022-08-27",
"TOTAL_DATA_REVENUE": "97825740.6",
"TOTAL_REVENUE_SM": "6731513.29",
"TOTAL_BUNDLE_REVENUE": "0"
}
}
Since you want an array containing the results and not a Map
instance, I'll use Array.from(lookup.values())
to convert the structure into the desired result.由于您想要一个包含结果的数组而不是
Map
实例,因此我将使用Array.from(lookup.values())
将结构转换为所需的结果。
Note that revenue.get(date)[names] = values
will replace the previous value with the same date
/ names
combination.请注意,
revenue.get(date)[names] = values
将用相同的date
/ names
组合替换以前的值。 That's why this answer only works if all the date
/ names
combinations are unique.这就是为什么这个答案只有在所有
date
/ names
组合都是唯一的情况下才有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.