0

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
          }
        ]
      }
    ]
  }
]
1
  • 1
    Why is your desired result an array if it only has one element? Commented Sep 6, 2022 at 4:18

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.