[英]Add an Array to another Array row
我有一个名为 Factories 的数组,为此我想添加每个工厂中可用的产品,然后组合两个数组,这样我就可以得到一个排序良好的数组。 我已经完成了最大的部分,但是我似乎无法将数组添加到每个单独的工厂
工厂数组(简化):
var F = [{ID: 2775030, name: "germany", products[0]},{ID: 2775031, name: "netherlands", product[1]}]
产品数组(简化)
var products = [{ID: 2775030, product: 458, ProductName: "blue"}, {ID: 2775030, product: 324, ProductName: "green"}, {ID: 2775031, product: 435, ProductName: "yellow"}]
想要的结果:
[
{
ID: 2775030,
name: "germany"
Product [2]
{
product: 458,
ProductName: "blue"
},
{ product: 324,
ProductName: "green"
}
},
{
ID: 2775031,
name: "netherlands"
Product [1]
{
product: 435,
ProductName: "yellow"
}
}
]
这就是我卡住的地方,我能够从数据库中删除产品数组,并且我得到了两个数组,但是我似乎无法将产品正确地推送到工厂列表中,所以我获得了新的更新产品工厂列表,这就是我得到的
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
}
}
您可以混合使用map
和filter
。
基本上映射所有的F
数组,并使用过滤器来构建products
..
例如。
var F = [{ID: 2775030, name: "germany"},{ID: 2775031, name: "netherlands"}]; var products = [{ID: 2775030, product: 458, ProductName: "blue"}, {ID: 2775030, product: 324, ProductName: "green"}, {ID: 2775031, product: 435, ProductName: "yellow"}]; const r = F.map(f=>({ ...f, products: products.filter(p => p.ID === f.ID) })); console.log(r);
var F = [
{ID: 2775030, name: "germany"},
{ID: 2775031, name: "netherlands"}
];
var products = [{ID: 2775030, product: 458, ProductName: "blue"}, {ID: 2775030, product: 324, ProductName: "green"}, {ID: 2775031, product: 435, ProductName: "yellow"}];
F.forEach((fact,i) => {
let filterData = products.filter(prd => prd.ID === fact.ID);
F[i].products = filterData;
});
console.log(JSON.stringify(F));
由于您的问题已经得到解答,因此我仅在您无法使用 ECMA6 脚本的情况下添加此内容(例如,因为您必须为 IE11 提供服务):
您不必进行太多更改,您可以将您的产品直接推送到您的目标阵列中:
function productFactorybuild(F) {
for (var i = 0; i < F.length; i++) {
for (var j = 0; j < FP.length; j++) {
if (F[i].ID == FP[j].ID) {
if (!F[i].Product)
F[i].Product= [];
F[i].Product.push
//PRODUCT
({
product: FP[j].id_product,
ProductName: FP[j].ProductName,
})
}
}
}
console.log(F) // THE DESIRED OUTPUT
}
我们可以创建一个对象来存储按ID
分组的产品。 按ID
分组可以通过 reduce 方法完成:
const groupedProducts = products.reduce((a, c)=> {
a[c.ID] = a[c.ID] || {products: []};
a[c.ID].products.push(c);
return a;
}, {});
然后只需map
您想要的对象并使用O(1)
访问groupedProducts
。 阅读更多关于大 O 符号的信息:
const r = F.map(f=>({
...f,
products: groupedProducts[f.ID] || []
}));
一个例子:
let F = [{ID: 2775030, name: "germany"},{ID: 2775031, name: "netherlands"}]; let products = [{ID: 2775030, product: 458, ProductName: "blue"}, {ID: 2775030, product: 324, ProductName: "green"}, {ID: 2775031, product: 435, ProductName: "yellow"}]; const groupedProducts = products.reduce((a, c)=> { a[c.ID] = a[c.ID] || {products: []}; a[c.ID].products.push(c); return a; }, {}); const r = F.map(f=>({ ...f, products: groupedProducts[f.ID] || [] })); console.log(r);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.