I am retrieving some data from PostgreSQL using node js API call. In this I am using 2 different pg tables. I am checking condition if "unit_code" is equal to "fk_unit_code" then it should combine the respective values from "Group_individuals" column and push it to the array. To get the "userData" I am doing left join between 2 tables. I have tried below code but its pushing single row per iteration of for loop.
function buildObject(userData){
for (var i = 0; i < userData.length; i++){
myArray = new Array() // at each iteration this will be empty and we get new group of "Group_individuals"
if(userData[i].unit_code=== userData[i].fk_unit_code){
myArray.push(userData[i].Group_individuals)
console.log(myArray)
}
}
}
Data available in "userData" is as below:
[
{ Group_individuals : 'John',
unit_code : 2,
fk_unit_code: 2,
}
{ Group_individuals : 'Rocky',
unit_code : 2,
fk_unit_code: 2,
}
{ Group_individuals : 'Alex',
unit_code : 3,
fk_unit_code: 3,
}
{ Group_individuals : 'Prince',
unit_code : 3,
fk_unit_code: 3,
}
{ Group_individuals : 'James',
unit_code : 2,
fk_unit_code: 2,
}
]
I want that myArray should look like: In first iteration myArray = [John, Rocky, James] In second iteration myArray = [Alex, Prince]