簡體   English   中英

如何按特定鍵對對象數組進行分組

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

我有一個包含兩個對象的數組。

我想通過一個鍵分配將兩個對象轉換為一個 object。 您可以看到分配字段現在將它們全部分組。 (它變成一個數組)

從分配:{} 到分配:[]

請問如何實現?

原始數組

[
  {
    "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
        }
      ]
    }
  }
]

預期數組

[
  {
    "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
          }
        ]
      }
    ]
  }
]

你可以使用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);

只需使用.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);

您可以通過以下方式使用 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