Is it a good approach to combine 3 arrays for following scenario

If I have the following data as shown in the JSFiddle here. For each children: and parent, I have separate Ajax Calls which are returning me an array. Is it possible to combine three arrays that would generate an array as shown in the fiddle?

Original source of above data is this and this is coming from here

More explanation of what I want:

I have 3 Arrays with me.

  1. One array has data with "EmployeeID": 2, in it.
  2. Second array has data containing "EmployeeID": 8,1,3,4 and 5 which is the child of first array.
  3. Third array containing EmployeeID": 6,7 and 9 which is the child of second array.

All of the above arrays I am getting via an Ajax call (not shown above) and wondering if I can combine it in such a way, such that I could generate data in the format shown above.
The thing that is puzzling me is how to add children: <my arra here> as shown in the above format.

Hi, one possible way to combine three arrays in JavaScript is to use the concat() method. For example, if you have three arrays like this:

var array1 = [{“EmployeeID”: 2}];

var array2 = [{“EmployeeID”: 8}, {“EmployeeID”: 1}, {“EmployeeID”: 3}, {“EmployeeID”: 4}, {“EmployeeID”: 5}];

var array3 = [{“EmployeeID”: 6}, {“EmployeeID”: 7}, {“EmployeeID”: 9}];

You can use concat() to combine them like this:

var combinedArray = array1.concat(array2, array3);

console.log(combinedArray); // logs the merged array

Thanks, Lara. But I’m kind of trying to build the opposite based on the three arrays such that it will satisfy the format that is used here for var employees here.

The var employees has this format with word children:[ showing below array1 and then below array2 :

How do I go about building it from the 3 arrays? Please let me know if my question made sense.

So you want to create a nested array from three flat arrays based on the parent-child relationship of the EmployeeID. The concat() method that I suggested earlier can only merge the arrays into one flat array, but it cannot create the nested structure that you want.

One possible way to create a nested array from flat arrays in JavaScript is to use a recursive function that loops through the arrays and checks the parent-child relationship of each item. For example, you can try something like this:

// Assuming you have three arrays like this:
var array1 = [{"EmployeeID": 2}];
var array2 = [{"EmployeeID": 8}, {"EmployeeID": 1}, {"EmployeeID": 3}, {"EmployeeID": 4}, {"EmployeeID": 5}];
var array3 = [{"EmployeeID": 6}, {"EmployeeID": 7}, {"EmployeeID": 9}];

// A function that takes an array and a parent ID and returns a nested array
function getNestedArray(array, parent) {
  var nestedArray = [];
  for (var i = 0; i < array.length; i++) {
    var item = array[i];
    // Check if the item's EmployeeID is equal to the parent ID
    if (item.EmployeeID == parent) {
      // Create an object with the item's properties and an empty children array
      var obj = {
        EmployeeID: item.EmployeeID,
        children: []
      };
      // Recursively call the function with the next array and the item's EmployeeID as the new parent ID
      // Concatenate the result to the obj's children array
      if (array == array1) {
        obj.children = obj.children.concat(getNestedArray(array2, item.EmployeeID));
      } else if (array == array2) {
        obj.children = obj.children.concat(getNestedArray(array3, item.EmployeeID));
      }
      // Push the obj to the nested array
      nestedArray.push(obj);
    }
  }
  return nestedArray;
}

// Call the function with the first array and 0 as the initial parent ID
var nestedArray = getNestedArray(array1, 0);
console.log(nestedArray); // logs the nested array
2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.