Home > Enterprise >  How to group an array of objects by a specific key
How to group an array of objects by a specific key

Time:09-06

I have a array of two objects.

I would like to transforming two objects into a single object generically by a key - allocation. 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
          }
        ]
      }
    ]
  }
]

CodePudding user response:

You can do this using 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);

CodePudding user response:

Just use .map() to extract the allocation property from each element of the array, and put that into the allocation property of the result.

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);

CodePudding user response:

You could use map method in below way:

    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)}]
  • Related