【问题标题】:Merge multiple objects with different format in node.js在node.js中合并多个不同格式的对象
【发布时间】:2020-01-23 03:53:09
【问题描述】:

我在下面创建对象,

products = [
    {id:1, name: a},
    {id:2, name: b},
]

prices = [
    {id:1, month:1, price:1},
    {id:1, month:2, price:1},
    {id:2, month:1, price:1},
    {id:2, month:2, price:1},
]

所以我希望输出为产品价格,

product_prices = [
    {id:1, name: a, prices: [{id:1, month:1, price:1},{id:1, month:2, price:1}]}, 
    {id:2, name: b, prices: [{id:2, month:1, price:1},{id:2, month:2, price:1}]},
]

下面是我尝试的代码

let subscriptionProductWithPrices = []


    subscriptionProducts.forEach(x => {
      subscriptionProductMonthPrices.forEach(y => {
        if (x.sub_product_id === y.sub_product_id) {
          subscriptionProductWithPrices = [
            {
              id: x.sub_product_id,
              name: x.sub_product_name,
              unit_id: x.facility_units_name,
              price: x.total_price,
              quantity: x.sub_product_quantity,
              recurring: x.sub_product_recurring,
              in_agreement: x.sub_product_in_agreement,
              start_date: x.sub_product_start_date,
              end_date: x.sub_product_end_date,
              prices:
                subscriptionProductMonthPrices


            }
          ]
          return subscriptionProductWithPrices
        }
      })
    })

此代码仅返回包含所有价格的最后一个产品 ID,但我希望输出如下,

    product prices [{id:1, name: a, prices[{{id:1, month:1, price:1},{id:1, month:2, price:1}]}, {id:2, name: b, prices[{{id:2, month:1, price:1},{id:2, month:2, price:1}]}]

有人可以帮我吗?

【问题讨论】:

  • 将 "subscriptionProductWithPrices = [" 替换为 subscriptionProductWithPrices.push( 因为您想在匹配时添加到数组中,而不是将其设置为仅当前匹配。这就是为什么它只有我所看到的最后一个结果。可能还有其他一些问题,但让我们在您将所有结果都放入 subscriptionProductWithPrices 数组中后解决它们。
  • @Spangle 使用上面我的代码目前我得到这个结果产品价格 [{id:2, name: b, prices[{id:1, month:1, price:1},{id: 1,月:2,价格:1},{id:2,月:1,价格:1},{id:2,月:2,价格:1}]}]。它以所有价格返回最后一个产品

标签: javascript mysql node.js json


【解决方案1】:

@Nithin 您的代码很好,但它试图将 过滤后的价格数组 推入 product.prices 数组,这将导致数组变成数组而不是数组目的。所以一个简单的修改就可以了。

这是它的优化代码。

let products = [{id:1, name: 'a'}, {id:2, name: 'b'}];
let prices = [{id:1, month:1, price:1},{id:1, month:2, price:1}, {id:2, month:1, price:1}, {id:2, month:2, price:1}];
products.map(prod=>{
// filter returns an array which can be directly assigned to prices
  prod.prices=prices.filter(pric=>pric.id===prod.id);
});
//Print the result stored in products with its corresponding prices
console.log(products);

【讨论】:

    【解决方案2】:

    根据id字段合并产品和价格如下-

    let products = [{id:1, name: 'a'}, {id:2, name: 'b'}];
    let prices = [{id:1, month:1, price:1},{id:1, month:2, price:1}, {id:2, month:1, price:1}, {id:2, month:2, price:1}];
    
    // loop through the products list
    let newProductsWithPrices=products.map(product=> {
      product.prices=[];     // for each product create an empty prices array field
      product.prices.push(prices.filter(price=>price.id===product.id)) // add the matching prices into the prices field
      return product;
    });
    
    console.log(newProductsWithPrices);
    // "[{"id":1,"name":"a","prices":[[{"id":1,"month":1,"price":1},{"id":1,"month":2,"price":1}]]},{"id":2,"name":"b","prices":[[{"id":2,"month":1,"price":1},{"id":2,"month":2,"price":1}]]}]"
    

    【讨论】:

      猜你喜欢
      • 2015-04-30
      • 2018-07-31
      • 2011-12-01
      • 2020-01-18
      • 1970-01-01
      • 2022-01-25
      • 2013-08-01
      • 2021-12-18
      • 2021-09-10
      相关资源
      最近更新 更多