Search code examples
javascriptlodash

Grouping objects js


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


Solution

  • 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>