0

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]
  }
})];

2 Answers 2

2

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);

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

2 Comments

thanks! I add some ternary on : resultObj.branches.push(value); to value !== id ? resultObj.branches.push(value) : [];
Hey @ARSetiawan I have edited it to match your required output
1

This is working fine as per your requirements. We are using a temporary hash map inside the format function that processes the input data array and returns the output.

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"
  }
];

function format(data = []) {
  const hashMap = {};
  const result = [];
  let resIndex = 0;
  data.forEach(thisData => {
    if (hashMap[thisData.data.client_id]) {
      result[hashMap[thisData.data.client_id]].branches.push(thisData.data.client_id);
    } else {
      hashMap[thisData.data.client_id] = resIndex++;
      result.push({
        id: thisData.data.client_id,
        branches: [thisData.value],
        all_branch: thisData.all_branch
      });
    }
  });
  return result;
}

console.log(format(siteLists));

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.