0

I have an array collecting objects like

myArray = [ 
           { lid_id: "1_1", et: "car", ty: "STS", dc: null, c: null, cp: null, im: false},
           { lid_id: "1_1", et: "car", ty: "STS", dc: 7, c: 11333, cp: 60, im: true},
           { lid_id: "1_2", et: "car", ty: "STC", dc: null, c: null, cp: null, im: false},
           { lid_id: "10_14473", et: "truck", ty: null, dc: 7, c: 14869, cp: 40, im: true}
          ]

I want to build new objects and colllect dc, c, cp and im keys if lid_id has the same value. For example following object which has key-value "lid_id":"1_1" collects the elements under ce. How can I collect these elements if lid_id is same?

myArray.forEach(function(data){
    "body": { "lid_id": "1_1",
              "et": "car", 
              "ty": "STS",
              "ce": [ 
                      { 
                        "dc": null, 
                        "c": null, 
                        "cp": null, 
                        "im": false
                      },
                      {
                        "dc": 7, 
                        "c": 11333, 
                        "cp": 60, 
                        "im": true
                      } 
                    ]
            },
     "body": { "lid_id": "1_2",
              "et": "car", 
              "ty": "STC",
              "ce": [ 
                      { 
                        "dc": null, 
                        "c": null, 
                        "cp": null, 
                        "im": false
                      }
                    ]
            },
     "body": { "lid_id": "10_14473",
               ...............
             }
})
1
  • 1
    As far as I understand the expected result, the structure you are proposing does not work, because there cant be multiple "body": { ... }" entries on the same level. Commented Sep 19, 2018 at 12:07

2 Answers 2

2

My method is using reduce to achieve the output.

const myArray = [{
    lid_id: "1_1",
    et: "car",
    ty: "STS",
    dc: null,
    c: null,
    cp: null,
    im: false
  },
  {
    lid_id: "1_1",
    et: "car",
    ty: "STS",
    dc: 7,
    c: 11333,
    cp: 60,
    im: true
  },
  {
    lid_id: "1_2",
    et: "car",
    ty: "STC",
    dc: null,
    c: null,
    cp: null,
    im: false
  },
  {
    lid_id: "10_14473",
    et: "truck",
    ty: null,
    dc: 7,
    c: 14869,
    cp: 40,
    im: true
  }
];

const grouping = myArray.reduce((acc, curr) => {
  const foundIndex = acc.findIndex(a => a.lid_id === curr.lid_id);
  const {
    dc,
    c,
    cp,
    im,
    lid_id,
    et,
    ty
  } = curr;
  const ceItem = {
    dc,
    c,
    cp,
    im
  };
  
  if (foundIndex > -1) {
    acc[foundIndex].ce.push(ceItem);
    return acc;
  }

  return [
    ...acc,
    {
      lid_id,
      et,
      ty,
      ce: [
        ceItem
      ]
    }
  ]
}, []);

console.log(grouping);

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

Comments

1

The first step is to pre-filter the data by the ID into a map containing an array of sub entries:

let myArray = [ 
           { lid_id: "1_1", et: "car", ty: "STS", dc: null, c: null, cp: null, im: false},
           { lid_id: "1_1", et: "car", ty: "STS", dc: 7, c: 11333, cp: 60, im: true},
           { lid_id: "1_2", et: "car", ty: "STC", dc: null, c: null, cp: null, im: false},
           { lid_id: "10_14473", et: "truck", ty: null, dc: 7, c: 14869, cp: 40, im: true}
          ];

console.log(myArray);

let myMap = [[]];

myArray.forEach( e =>
    {
            let internalArray = myMap[e.lid_id];
      console.log("internal: " + internalArray);

      if(internalArray == undefined){
         myMap[e.lid_id] = [];      
      }

            myMap[e.lid_id].push(e);

    }
);

console.log(myMap);

After that, it's just simple to do some further processing in order to get the appropriate groupings.

Comments

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.