i have an Array called Factories and to that i would like to add the products available in each factory and then combine the two arrays so i get one nicely sorted array out. I have the biggest part done, however i can't seem to get the array added to each individual factory
factory array (simplyfied):
var F = [{ID: 2775030, name: "germany", products[0]},{ID: 2775031, name: "netherlands", product[1]}]
products array (simplied)
var products = [{ID: 2775030, product: 458, ProductName: "blue"}, {ID: 2775030, product: 324, ProductName: "green"}, {ID: 2775031, product: 435, ProductName: "yellow"}]
desired result:
[
{
ID: 2775030,
name: "germany"
Product [2]
{
product: 458,
ProductName: "blue"
},
{ product: 324,
ProductName: "green"
}
},
{
ID: 2775031,
name: "netherlands"
Product [1]
{
product: 435,
ProductName: "yellow"
}
}
]
this is where i got stuck, i am able to remove the product array as from database and i get both arrays, but i can't seem to push the product correctly inside the factory listing, so i get the new updated product factory listing, this is what i got
var products = [{ID: 2775030, product: 458, ProductName: "blue"}, {ID: 2775030, product: 324, ProductName: "green"}, {ID: 2775031, product: 435, ProductName: "yellow"}]
ListProductsAvailable(products)
function ListProductsAvailable(products) {
var F = [{ID: 2775030, name: "germany"},{ID: 2775031, name: "netherlands"}]
var FP = products;
// remove the Product from the existing array
for (var i = 0; i < F.length; i++) {
delete F[i].Product;
if (i == F.length -1) {
productFactorybuild(F)
}
}
function productFactorybuild(F) {
for (var i = 0; i < F.length; i++) {
Product = [];
for (var j = 0; j < FP.length; j++) {
if (F[i].ID == FP[j].ID) {
Product.push
//PRODUCT
({
product: FP[j].id_product,
ProductName: FP[j].ProductName,
})
}
}
F.push(Product);
}
console.log(F) // THE DESIRED OUTPUT
}
}