简体   繁体   English

如何通过比较其中的一个 object 来合并对象数组

[英]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.

相关问题 比较两个数组对象数据并在JavaScript中将一个对象合并为一个数组? - Comparing two array objects data and merge as one object into array in javascript? 如何在一个对象(包括内部对象)中合并对象数组-JavaScript? - How to merge array of objects in one object, including inner objects - JavaScript? javascript - 将对象数组中的 2 个对象合并为一个 Object - javascript - Merge 2 objects in Array of Objects into one Object 将一个数组中的两个对象合并为一个数组中的object - Merge two objects in an array into one object in array 如何将具有相同键的两个对象(关联数组)合并到一个对象中 - How to merge two objects (associative array) with same key into one object 将一个数组的对象按键合并为一个object - Merge objects of an array into one object by key 将包含对象数组的 object 与简单数组进行比较 - Comparing one object containing array of objects with simple array 通过比较数组内的对象数组来合并数组 - Merge an array by comparing the array of objects inside the array 如何合并对象数组和另一个对象数组 - How to merge array of object and another array of objects 通过比较属性将第二个数组与第一个数组合并,如果对象不是第二个数组的一部分,则将属性添加到第一个数组中的对象 - merge second array with first array by comparing properties and add properties to objects in first array if object is not part of second array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM