-1

This is my input:

    companyList: [
      {
        company_name: 'company1',
        item: 'item1'
      },
      {
        company_name: 'company1',
        item: 'item2'
      },
      {
        company_name: 'company1',
        item: 'item3'
      },
      {
        company_name: 'company2',
        item: 'item4'
      }
    ]

And this is how I want to be the output:

    result: [
      {
        company_name: 'company1',
        product: [
          { item: 'item1'},
          { item: 'item2'},
          { item: 'item3'}
        ]
      },
      {
        company_name: 'company2',
        product: [
          { item: 'item4'}
        ]
      }
    ]

Using map, how can I get my result?

Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
  • 2
    The posted question does not appear to include [any attempt](https://idownvotedbecau.se/noattempt/) at all to solve the problem. StackOverflow expects you to [try to solve your own problem first](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users), as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific problem you're having in a [MCVE]. For more information, please see [ask] and take the [tour]. – CertainPerformance Oct 18 '18 at 06:22
  • This may help you through part: [How to group an array of objects by key](https://stackoverflow.com/questions/40774697/how-to-group-an-array-of-objects-by-key) – Jonathan Lonowski Oct 18 '18 at 06:27
  • https://www.google.nl/search?q=javascript+group+object+site:stackoverflow.com – mplungjan Oct 18 '18 at 06:28

2 Answers2

0

You can loop over the array of companyList and get that desired output using O(n) complexity as belows:

var companyList = [{
  company_name: 'company1',
  item: 'item1'
}, {
  company_name: 'company1',
  item: 'item2'
}, {
  company_name: 'company1',
  item: 'item3'
}, {
  company_name: 'company2',
  item: 'item4'
}];

var tempObj = {};
companyList.forEach((companyObj) => {
  if(tempObj[companyObj.company_name]){
    tempObj[companyObj.company_name].product.push({item: companyObj.item});
  } else {
    tempObj[companyObj.company_name] = {
      company_name: companyObj.company_name,
      product: [{item: companyObj.item}]
    }
  }
});
var result = Object.values(tempObj);
console.log(result);
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
0

You can use reduce to create new array

let companyList = [{
    company_name: 'company1',
    item: 'item1'
  },
  {
    company_name: 'company1',
    item: 'item2'
  },
  {
    company_name: 'company1',
    item: 'item3'
  },
  {
    company_name: 'company2',
    item: 'item4'
  }
]

let result = companyList.reduce(function(acc, curr) {
  // use findIndex to get the index of the object where the comany_name matches
  let findIndex = acc.findIndex(function(item) {
    return item.company_name === curr.company_name;
  });
  // will get -1 is company_name does not exist
  if (findIndex === -1) {
    let obj = {};
    // then create new object and push the value to it
    obj.company_name = curr.company_name;
    obj.product = [{
      item: curr.item
    }];
    acc.push(obj)
  } else {
    // if company_name exist then update the product list
    acc[findIndex].product.push({
      item: curr.item
    })
  }
  return acc;
}, []);
console.log(result)
brk
  • 48,835
  • 10
  • 56
  • 78