Search code examples
javascriptarraysobjectconcatenationreduce

concat same id within object to one array of object


I have data which is structured like this:

let siteLists = [
    {
        "data": {
            "client_id": 29
        },
        "all_branch": 1,
        "value": 29,
        "label": "A Site"
    },
    {
        "data": {
            "client_id": 23,
        },
        "all_branch": 0,
        "value": 91,
        "label": "B-1 Site"
    },
    {
        "data": {
            "client_id": 23,
        },
        "all_branch": 0,
        "value": 86,
        "label": "B-2 Site"
    },
    {
        "data": {
            "client_id": 10
        },
        "all_branch": 1,
        "value": 10,
        "label": "C Site"
    }
];

I need to concatenate the above object which has same data.client_id value into one object which contains all branch values.

Expected result:

[
  {
    id: 29, 
    branches: [],
    all_branch:1
  },
  {
    id: 23, 
    branches: [91,86],
    all_branch:0
  },
  {
    id: 10,
    branches: [],
    all_branch:1
  }
]

I tried the following code but the result did not fit the expected result...

let populate = [...siteLists.map(item => {
  return item.all_branch === 1 ? {
    all_placement: 1,
    placement_id: item.data.client_id,
    branch_id: []
  } : {
    all_placement: 0,
    placement_id: item.data.client_id,
    branch_id: [item.value]
  }
})];

Solution

  • This should work, may not be optimal though. I also took the liberty to add the values to branches array instead of leaving it empty.

    let siteLists = [{
        "data": {
          "client_id": 29
        },
        "all_branch": 1,
        "value": 29,
        "label": "A Site"
      },
      {
        "data": {
          "client_id": 23,
        },
        "all_branch": 0,
        "value": 91,
        "label": "B-1 Site"
      },
      {
        "data": {
          "client_id": 23,
        },
        "all_branch": 0,
        "value": 86,
        "label": "B-2 Site"
      },
      {
        "data": {
          "client_id": 10
        },
        "all_branch": 1,
        "value": 10,
        "label": "C Site"
      }
    ];
    var resultarray = []; //expected result
    var branchId = []; //stores an object to map the branch ID and the array index to keep track of duplicate IDs
    var count = 0; //keeps track of the current array Index
    siteLists.forEach(element => { //for loop to loop through siteList array
      var resultObj = new Object(); //stores the temporary result Object
      var id = element.data.client_id;
      var status = false; //keeps track of if it is a duplicate ID
      if (count != 0) {
        branchId.forEach(obj => { //for loop to check for duplicate ID (will only run after the first iteration of result loop)
          if (obj.id === id) { //checks if the ID matches
            var index = obj.index;
            var value = element.value;
            resultarray[index].branches.push(obj.value);//pushes the matched ID value
            resultarray[index].branches.push(value); //Pushes value to branches array if ID is duplicate
            status = true;
          }
        })
      }
      if (status == false) {
        var branchObj = {
          'id': id,
          'index': count,
          'value':element.value
        }
        var allBranch = element.all_branch;
        //var value = element.value; //uncomment if you need value in the resultObj when id does not match
        branchId.push(branchObj);
        resultObj.id = id;
        resultObj.branches = [];
        //resultObj.branches.push(value);//uncomment if you need value in the resultObj when id does not match
        resultObj.all_branch = allBranch;
        resultarray.push(resultObj);
        count = count + 1;
      } else {
        status = false;
      }
    
    })
    console.log(resultarray);