Hello
I have 2 arrays returned from the two SQL servers. I would like to build a new array I will feed to the dashboard table which will show the comparison between result of the queries.
I get to the point where I create a 3rd array which is a list of results without duplicates and two arrays formatted to the same structure.
Code below from the moment when 3 arrays are formatted:
var arr = [];
var obj_arr = [{"process":"A"},{"process":"B"},{"process":"C"},{"process":"D"}];
var arr1 = [{"process":"C","qty":88,"origin":"MMS"},{"process":"B","qty":11,"origin":"MMS"},{"process":"A","qty":8,"origin":"MMS"}]
var arr1_length = arr1.length;
var arr2 = [{"process":"A","qty":280,"origin":"SP"},{"process":"D","qty":6,"origin":"SP"},{"process":"C","qty":9,"origin":"SP"}]
var arr2_length = arr2.length;
//Sort arr1 (results are sorted on SQL, but I want this to be future proof)
arr1.sort(function(a, b){
var x = a.process.toLowerCase();
var y = b.process.toLowerCase();
if (x < y) {return -1;}
if (x > y) {return 1;}
return 0;
});
//Sort arr2
arr2.sort(function(a, b){
var x = a.process.toLowerCase();
var y = b.process.toLowerCase();
if (x < y) {return -1;}
if (x > y) {return 1;}
return 0;
});
//assign values from arr1 to "processes" from obj_arr
var arr3 = obj_arr.map((item, i) => Object.assign({}, item, arr1[i]));
//truncate to arr1_length
arr3 = arr3.slice(0,arr1_length);
//assign values from arr2 to "processes" from obj_arr
var arr4 = obj_arr.map((item, i) => Object.assign({}, item, arr2[i]));
//truncate to arr2_length
arr4 = arr4.slice(0,arr2_length);
obj_arr.forEach(BuildList); //build list of processes
var process;
var arr3qty;
var arr4qty;
var z = 0;
function BuildList (obj_arr,z,arr3,arr4) {
for (i=0; i < obj_arr.length; i++) {
if (obj_arr.process == arr3.process){
arr3qty = arr3.qty;
} else {
arr3qty = "test1" // change to null after tetsing;
}
if (obj_arr.process == arr4.process){
arr4qty = arr4.qty;
} else {
arr34ty = "test2" // change to null after tetsing;
}
}
process = obj_arr.process;
arr[z]=[z+1,process, arr3qty, arr4qty]; // build an array
z = z+1;
msg.payload = arr;
}
/*
Expected Structure:
[[1,"A",arr3.qty, arr4.qty],[2,"B",arr3.qty, arr4.qty],[3,"C",arr3.qty, arr4.qty],[4,"D",arr3.qty, arr4.qty]]
Expected Output:
[[1,"A",8,280],[2,"B",11,null],[3,"C",88,9],[4,"D",null,6]]
Actual Output:
[[1,"A",null,null],[2,"B",null,null],[3,"C",null,null],[4,"D",null,null]]
*/
msg.obj_arr = obj_arr;
msg.arr1 = arr1;
msg.arr2 = arr2;
msg.arr3 = arr3;
msg.arr4 = arr4;
return msg;
Please can somebody help me with getting function BuildList working.
What I'm trying to achieve there is to use obj_arr as a list, which will finally be displayed in the 2nd column of the table and assign corresponding values from the arr3 and arr4.
I know that the if else statements of the function doesn't work, but I don't know why.
I should mention I'm fairly new to programming.
Example of the table I want to build: