简体   繁体   中英

Group list of objects by JSON key

I am trying to reformat my list of objects by grouping a certain key pair in javascript.

Data Format

[{
    "sccode": "sccode1",
    "val": "1ADA"
}, {
    "sccode": "sccode2",
    "val": "1ADB"
}, {
    "sccode": "sccode1",
    "val": "1ADC"
}]

Expected Result

[{
        "scCode": "sccode1",
        "valCodes": ["1ADA", "1ADC"]
    }, 
{
        "scCode": "sccode2",
        "valCodes": ["1ADB"]
    }

]

I believe I could loop through the array and match my keys, but is there a quick way to reformat this without having to explicitly loop through? I've tried using a reduce function below, but it gives undefined errors with find, which i think has something to do with my formatting.

Tried (?) Code

 const resp = data.reduce((acc, ele) => {
          const ant = acc.find(x => x.sccode === ele.sccode);
        }, []);

Would this do?

 const src = [{"sccode":"sccode1","val":"1ADA"},{"sccode":"sccode2","val":"1ADB"},{"sccode":"sccode1","val":"1ADC"}], result = src.reduce((r,{sccode,val}) => { const match = r.find(({scCode}) => scCode == sccode) match ? match.valCodes.push(val) : r.push({scCode:sccode, valCodes: [val]}) return r }, []) console.log(result)
 .as-console-wrapper{min-height:100%;}

Try the following, I use a map to store a partial state to improve performances preventing to search sccode in an array for every initial object.

let partial = [{
  "sccode": "sccode1",
  "val": "1ADA"
}, {
  "sccode": "sccode2",
  "val": "1ADB"
}, {
  "sccode": "sccode1",
  "val": "1ADC"
}].reduce((map, obj) => {
  if (!map[obj.sccode]) {
    map[obj.sccode] = [obj.val];
  } else {
    map[obj.sccode].push(obj.val);
  }
  return map;
}, {})

Object.keys(partial).map(sccode => ({
  sccode, valCodes: partial[sccode]
}));

try loaddash/groupby

let groupByResult = groupBy(data, function (n) {
          return n.sccode
        });

Check this code:

    array.reduce(function(res, value){
        if(!res[value.sccode]) {
            res[value.sccode] = value; 
            res[value.sccode]['valCodes'] = []
            result.push(res[value.sccode]);
        }
        res[value.sccode]['valCodes'].push(value.val); 
        return res; 
    } ,{}); 

I tested here and it works fine!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM