Search code examples
javascriptjsonnestedlodash

How to use lodash to group multi-nested JSON article?


I'm trying to figure out how to group my JSON article using lodash with mapping and grouping functions. I'm pretty new to lodash and javascript in general but I've gotten close but cannot figure out the last bit of formatting!

What I've Tried

    const data = [{
      "pcc": "1ADA",
      "markets": {
        "origin": "ORG",
        "destination": "DES"
      }
    }, {
      "pcc": "1ADA",
      "markets": {
        "origin": "ORD",
        "destination": "DES"
      }
    },{
      "pcc": "1ADA",
      "markets": {
        "origin": "ORG",
        "destination": "DES"
      }
    }, {
      "pcc": "1AZA",
      "markets": {
        "origin": "ORG",
        "destination": "DES"
      }
    }, {
      "pcc": "1AXA",
      "markets": {
        "origin": "ORG",
        "destination": "DES"
      }
    }]

    excelDef = _(data)
      .groupBy("pcc")
      .map((group, pcc) =>({
        pcc,
        markets: group.map(({origin, markets: destination}) => ({
          origin,
          destination
        }))
      }))
      .value();

Output that I am getting

 [{
      "pcc": "1ADA",
      "markets": [{
        "destination": {
          "origin": "ORG",
          "destination": "DES"
        }
      }, {
        "destination": {
          "origin": "ORD",
          "destination": "DES"
        }
      }, {
        "destination": {
          "origin": "ORG",
          "destination": "DES"
        }
      }]
    }, {
      "pcc": "1AZA",
      "markets": [{
        "destination": {
          "origin": "ORG",
          "destination": "DES"
        }
      }]
    }, {
      "pcc": "1AXA",
      "markets": [{
        "destination": {
          "origin": "ORG",
          "destination": "DES"
        }
      }]
    }]

Desired Output

[{
"pcc": "1ADA",
"markets": [{
    "origin": "ORG",
    "destination": "DES"
}, {
    "origin": "ORD",
    "destination": "DES"
}, {
    "origin": "ORG",
    "destination": "DES"
}]
}, {
 "pcc": "1AZA",
 "markets": [{
    "origin": "ORG",
    "destination": "DES"
}]
}, {
 "pcc": "1AXA",
 "markets": [{
    "origin": "ORG",
    "destination": "DES"
  }]
}]

I feel like I am close to my desired output, but the syntax of the mapping and grouping function always throws me off as someone who is starting out programming. Any help would be appreciated!


Solution

  • In a different approach, without lodash, you can use reduce:

    const data=[{"pcc":"1ADA","markets":{"origin":"ORG","destination":"DES"}},{"pcc":"1ADA","markets":{"origin":"ORD","destination":"DES"}},{"pcc":"1ADA","markets":{"origin":"ORG","destination":"DES"}},{"pcc":"1AZA","markets":{"origin":"ORG","destination":"DES"}},{"pcc":"1AXA","markets":{"origin":"ORG","destination":"DES"}}]
    
    const resp = data.reduce((acc, ele) => {
        const ant = acc.find(x => x.pcc === ele.pcc);
        if(!ant) return acc.concat({pcc: ele.pcc, markets: [ele.markets]});
        ant.markets.push(ele.markets);
        return acc;
    }, []);
    
    console.log(resp);