0

I'm just learning and I can't group the object

I need to group objects by the "product" field and filter by the "product" field in the "keyResults" and "metrics" fields

I know that this is done using the reduce function, but I can't, I also tried using the Lodash sortby function, but this is not what I need

Help please:(

const a = {
  products: {
    keyResults: [
      {
        leader: "Aaron",
        product: "Product 1"
      },
      {
        leader: "Addie",
        product: "Product 1"
      },
      {
        leader: "Mindy",
        product: "Product 1"
      },
      {
        leader: "Wiley",
        product: "Product 2"
      },
    ],
    metrics: [
      {
        leader: "Aaron",
        product: "Product 1"
      },
      {
        leader: "Wiley",
        product: "Product 2"
      }
    ]
  }
};

Here's what I expect

const b = {
  products: [
    {
      product: "Product 1",
      keyResults: [
        {
          leader: "Aaron",
          product: "Product 1"
        },
        {
          leader: "Addie",
          product: "Product 1"
        },
        {
          leader: "Mindy",
          product: "Product 1"
        }
      ],
      metrics: [
        {
          leader: "Aaron",
          product: "Product 1"
        },
      ]
    },
    {
      product: "Product 2",
      keyResults: [
        {
          leader: "Wiley",
          product: "Product 2"
        },
      ],
      metrics: [
        {
          leader: "Wiley",
          product: "Product 2"
        }
      ]
    }
  ]
};

This is a question about how to make a large object from an array, where there will be grouping by one of the elements of the array, but at the same time everything was elements of the array, not the object

darfil
  • 11
  • 3
  • 1
    OK. So here's what you have, here's what you expect, but what have you tried? You should add your attempts at solving this to your question as a [mcve]. – Andy Aug 28 '23 at 21:37
  • or [Group array items using object](https://stackoverflow.com/questions/31688459/group-array-items-using-object) or [Most efficient method to groupby on an array of objects](https://stackoverflow.com/questions/14446511/most-efficient-method-to-groupby-on-an-array-of-objects) – pilchard Aug 28 '23 at 21:56
  • https://codesandbox.io/s/nice-yalow-nkdtdl?file=/src/index.mjs this is my minimal solution, but it doesn't work – darfil Aug 29 '23 at 07:52

1 Answers1

0

Collect products into an object with properties as product names and use Object.values() to get products as an array:

const products = {};

for(const key of ['keyResults', 'metrics']){
  for(const product of a.products[key]){
    const id = product.product;
    if(!products[id]){
      products[id] = {product: id, keyResults: [], metrics: []};
    }
    products[id][key].push(product);
  }
}

const b = {products: Object.values(products)};

console.log(b);
<script>
const a = {
  products: {
    keyResults: [
      {
        leader: "Aaron",
        product: "Product 1"
      },
      {
        leader: "Addie",
        product: "Product 1"
      },
      {
        leader: "Mindy",
        product: "Product 1"
      },
      {
        leader: "Wiley",
        product: "Product 2"
      },
    ],
    metrics: [
      {
        leader: "Aaron",
        product: "Product 1"
      },
      {
        leader: "Wiley",
        product: "Product 2"
      },
      {
        leader: "Peter",
        product: "Product 3"
      }
    ]
  }
};
</script>
Alexander Nenashev
  • 8,775
  • 2
  • 6
  • 17