javascriptarraysobjectconcatenationarray-combine

loop through an array and compare object values to combine respective arrays


Here is my array , I need to loop through this array and check if objects with same docId exists and if they do, I have to combine jArray of these objects

FinalArray = [
    {
        "jArray": [
            {
                "Cd": "A"
            }
        ],
        "Ref": {
            "docId": "123"
        },
        "name": "bene",
        "check1": false,
        "check2": false,
        "check3": false,
        "check4": false,
        "id": "0001"
    },
    {
        "jArray": [
            {
                "Cd": "A"
            }
        ],
        "Ref": {
            "docId": "456"
        },
        "name": "leg",
        "check1": false,
        "check2": false,
        "check3": false,
        "check4": false,
        "id": "0001"
    },
    {
        "jArray": [
            {
                "Cd": "B"
            }
        ],
        "documentRef": {
            "docId": "123"
        },
        "name": "bene",
        "check1": false,
        "check2": false,
        "check3": false,
        "check4": false,
        "id": "0001"
    },
    {
        "jArray": [
            {
                "Cd": "B"
            }
        ],
        "Ref": {
            "docId": "456"
        },
        "name": "leg",
        "check1": false,
        "check2": false,
        "check3": false,
        "check4": false,
        "id": "0001"
  },
  {
        "jArray": [
            {
                "Cd": "A"
            }
        ],
        "Ref": {
            "docId": "789"
        },
        "name": "hello",
        "check1": false,
        "check2": false,
        "check3": false,
        "check4": false,
        "id": "0001"
    }
]

expected result is as below.

[
    {
        "jArray": [
            {
                "Cd": "A"
        },
        {
                "Cd": "B"
        }
        ],
        "Ref": {
            "docId": "123"
        },
        "name": "bene",
        "check1": false,
        "check2": false,
        "check3": false,
        "check4": false,
        "id": "0001"
    },
    {
        "jArray": [
            {
                "Cd": "A"
        },
        {
                "Cd": "B"
        }
        ],
        "Ref": {
            "docId": "456"
        },
        "name": "leg",
        "check1": false,
        "check2": false,
        "check3": false,
        "check4": false,
        "id": "0001"
    },
  {
        "jArray": [
            {
                "Cd": "A"
            }
        ],
        "Ref": {
            "docId": "789"
        },
        "name": "hello",
        "check1": false,
        "check2": false,
        "check3": false,
        "check4": false,
        "id": "0001"
    }
]

any thoughts of how to achieve this ? how do i loop through an array and compare object values to combine respective arrays

I've been trying to do something like below. but can't figure out the right way

FinalArray.map((object, index) => {
          if (object.Ref.docId === FinalArray[index + 1].Ref.docId) {
            const tempJArray = object.jArray.concat(FinalArray[index + 1].jArray);
Object.assign(tempJArray , jArray);
            Object.assign({}, object.Ref.docId, FinalArray[index + 1].Ref.docId);

          }
        });

Solution

  • Much similar to a recent answer by me about "group array of objects by key" using reduce

    var FinalArray = [{"jArray":[{"Cd":"A"}],"Ref":{"docId":"123"},"name":"bene","check1":false,"check2":false,"check3":false,"check4":false,"id":"0001"},{"jArray":[{"Cd":"A"}],"Ref":{"docId":"456"},"name":"leg","check1":false,"check2":false,"check3":false,"check4":false,"id":"0001"},{"jArray":[{"Cd":"B"}],"documentRef":{"docId":"123"},"name":"bene","check1":false,"check2":false,"check3":false,"check4":false,"id":"0001"},{"jArray":[{"Cd":"B"}],"Ref":{"docId":"456"},"name":"leg","check1":false,"check2":false,"check3":false,"check4":false,"id":"0001"},{"jArray":[{"Cd":"A"}],"Ref":{"docId":"789"},"name":"hello","check1":false,"check2":false,"check3":false,"check4":false,"id":"0001"}];
    
    var before = JSON.stringify(FinalArray);
    
    
    var obj = FinalArray.reduce(function(agg, item) {
      var docId = item.Ref && item.Ref.docId || "other"
      var copy = [...item.jArray];
      if (!agg[docId]) {
        agg[docId] = {
          jArray: [],
          Ref: { docId }, 
          name: item.name,
          id: item.id,
          // etc
        }
      }
      agg[docId].jArray = agg[docId].jArray.concat(copy)
    
      return agg;
    }, {})
    
    var result = Object.values(obj);
    console.log(result)
    
    var after = JSON.stringify(FinalArray);
    console.log(before===after)
    .as-console-wrapper {
      max-height: 100% !important;
    }