0

I have an array of objects as an input.

var val = [{matnr :'0001',type:'Z0001',price:12.3,location:'Afr'},{matnr :'0001',type:'Z0002',price:12.2,location:'US'},
,{matnr :'0002',type:'Z0003',price:11.2,location:'EU'}]

I need to remove location from each object and group by material.

val = [{
      matnr:0001
      types   :[{type:'Z001',price:12.3},{type:'Z001',price:12.2}]
     },
     {
      matnr:0002
      types   :[{type:'Z003',price:12.3}]
     }

I tried to delete an object from an array and did a group by but seems to be not working. Could you please help

    val.forEach((values)=> 
    Object.keys(values).forEach(function (item) {
         if (item !='matnr'||item !='type' || item != price){
           delete values[item];
         }; 
    }) 

    var grouped = _.groupBy(val, function(val) {
    return val.matnr;
  });

2 Answers 2

1

You can use .reduce() with destructuring to remove the location property and group attributes by matnr by creating an object, where each key is matnr, and each value is an accumulation of properties for that given matnr like so:

const arr = [{matnr:"0001",type:"Z0001",price:12.3,location:"Afr"},{matnr:"0001",type:"Z0002",price:12.2,location:"US"},{matnr:"0002",type:"Z0003",price:11.2,location:"EU"}];

const res = Object.values(arr.reduce((acc, {matnr, type, price}) => {
  const {types = []} = acc[matnr] || {}; 
  acc[matnr] = {matnr, types: [...types, {type, price}]};
  return acc;
}, Object.create(null)));
console.log(res);
.as-console-wrapper {max-height: 100% !important;}

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

Comments

1

you can do this without using loadash simply by using the higher-order functions

const cleaned = removeFromList(list).key('location')
const grouped = group(list).by('matnr')


function removeFromList(arr) {

  return {
    key: key => arr.map(item => {
      if (!item[key]) return item
      delete item[key]
      return item
    })
  }
}

function group(arr) {
  return {
    by: groupKey => {
      const groupsObj = arr.reduce((groups, item) => {
        const groupKeyValue = item[groupKey]
        if(!groupKeyValue) return groups

        if (!groups[groupKeyValue]){
          groups[groupKeyValue] = [item]
          return groups
        }

        groups[groupKeyValue] = [...groups[groupKeyValue], item]

        return groups
      }, {});

      return groupsObj
    }
  }
}

Note We Prefer the object structures for performance and easy access as developers, so on the group by function we return the grouped object with the materials values as keys and the matches as their values. Like :

{
  0001 : [{type:'Z001',price:12.3},{type:'Z001',price:12.2}]
  0002 : [{type:'Z003',price:12.3}]
}

example available here on repl.it

1 Comment

an edit I made a mistake on the first code snippet, I was using the group key as the unique identifier instead I should useded the group key value from the items themselves!

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.