简体   繁体   English

如何按特定键对对象数组进行分组

[英]How to group an array of objects by a specific key

I have a array of two objects.我有一个包含两个对象的数组。

I would like to transforming two objects into a single object generically by a key - allocation.我想通过一个键分配将两个对象转换为一个 object。 You can see that allocation fields is now grouping them all.您可以看到分配字段现在将它们全部分组。 (it becomes a array) (它变成一个数组)

From allocation: {} to allocation:[]从分配:{} 到分配:[]

May I ask how to achieve that?请问如何实现?

Original array原始数组

[
  {
    "allocation": {
      "name": "custom",
      "customAllocations": [
        {
          "name": "Developed",
          "weight": 0.75
        },
        {
          "name": "Diversified",
          "weight": 0.1
        },
        {
          "name": "Global",
          "weight": 0.15
        }
      ]
    }
  },
  {
    "allocation": {
      "name": "custom",
      "customAllocations": [
        {
          "name": "Developed",
          "weight": 0.35
        },
        {
          "name": "Conservative",
          "weight": 0.1
        },
        {
          "name": "Global",
          "weight": 0.55
        }
      ]
    }
  }
]

Expected array预期数组

[
  {
    "allocation": [
      {
        "name": "custom",
        "customAllocations": [
          {
            "name": "Developed",
            "weight": 0.75
          },
          {
            "name": "Diversified",
            "weight": 0.1
          },
          {
            "name": "Global",
            "weight": 0.15
          }
        ]
      },
      {
        "name": "custom",
        "customAllocations": [
          {
            "name": "Developed",
            "weight": 0.35
          },
          {
            "name": "Conservative",
            "weight": 0.1
          },
          {
            "name": "Global",
            "weight": 0.55
          }
        ]
      }
    ]
  }
]

You can do this using Array.prototype.reduce :你可以使用Array.prototype.reduce来做到这一点:

 const data = [ { "allocation": { "name": "custom", "customAllocations": [ { "name": "Developed", "weight": 0.75 }, { "name": "Diversified", "weight": 0.1 }, { "name": "Global", "weight": 0.15 } ] } }, { "allocation": { "name": "custom", "customAllocations": [ { "name": "Developed", "weight": 0.35 }, { "name": "Conservative", "weight": 0.1 }, { "name": "Global", "weight": 0.55 } ] } } ]; const newData = [data.reduce((acc, curr) => { acc.allocation.push(curr.allocation); return acc; }, { allocation: [] })]; console.log(newData);

Just use .map() to extract the allocation property from each element of the array, and put that into the allocation property of the result.只需使用.map()从数组的每个元素中提取allocation属性,并将其放入结果的allocation属性中。

 const original = [{ "allocation": { "name": "custom", "customAllocations": [{ "name": "Developed", "weight": 0.75 }, { "name": "Diversified", "weight": 0.1 }, { "name": "Global", "weight": 0.15 } ] } }, { "allocation": { "name": "custom", "customAllocations": [{ "name": "Developed", "weight": 0.35 }, { "name": "Conservative", "weight": 0.1 }, { "name": "Global", "weight": 0.55 } ] } } ]; const result = [{ allocation: original.map(el => el.allocation) }]; console.log(result);

You could use map method in below way:您可以通过以下方式使用 map 方法:

    const originalArr = [
  {
    "allocation": {
      "name": "custom",
      "customAllocations": [
        {
          "name": "Developed",
          "weight": 0.75
        },
        {
          "name": "Diversified",
          "weight": 0.1
        },
        {
          "name": "Global",
          "weight": 0.15
        }
      ]
    }
  },
  {
    "allocation": {
      "name": "custom",
      "customAllocations": [
        {
          "name": "Developed",
          "weight": 0.35
        },
        {
          "name": "Conservative",
          "weight": 0.1
        },
        {
          "name": "Global",
          "weight": 0.55
        }
      ]
    }
  }
]

const output = [{"allocation": originalArr.map(item => item.allocation)}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM