【问题标题】:javascript find nested objects in another array and do mathjavascript 在另一个数组中查找嵌套对象并进行数学运算
【发布时间】:2021-10-14 17:29:49
【问题描述】:

我需要从 'accessories' 数组中的 'products.accessories' 数组中找到两个嵌套对象,并检查它们的货币以乘以奇偶校验,检查它们的单位以乘以 'products.properties.length',将每个对象乘以它们'qty' 并将它们的结果相加。

非常感谢任何建议。谢谢。

var accessories = [{
  name: 'Accessory-1',
  currency: 'eur',
  price: 1,
  unit: 'm'
},
{
  name: 'Accessory-2',
  currency: 'usd',
  price: 2,
  unit: 'pcs'
}];

var products = [{
  name: 'Product',
  properties: [{
    no: 'Product-1',
    length: '2000',
    accessories: [{
      name: 'Accessory-1',
      qty: 1
    },
    {
      name: 'Accessory-2',
      qty: 1
    }]
  }]
}];
// currencies
let eurusd = 1.18
let gbpusd = 1.38

编辑:谢谢@AlexeyZelenin

    function getPrice(accessory) {
        accessory.unit === 'm' ? unitMultiplier = length / 1000 : unitMultiplier = 1
        return (
            accessory.currency === 'eur' ? (accessory.price * eurusd) : 
                (accessory.currency === 'gbp' ? (accessory.price * gbpusd) : 
                        accessory.price)
                        ).toFixed(2)
    }
    function getAcc(property) { 
        return property.accessories 
            .map(x => x.qty * getPrice(accessories.find(p => p.name === x.name)) * unitMultiplier)
            .reduce((c, p) => c + p)
    }

帕格:

each i in products
     .....
     each j in i.properties
          .....
          length = j.length
          .....

【问题讨论】:

  • “product.currency”字段在哪里?
  • @AlexeyZelenin 产品没有货币,是美元,我只需要把配件价格换成和产品一样的美元。
  • 你的函数getPrice()声明,应该有pricecurrency字段。

标签: javascript pug


【解决方案1】:

// Accessories: each accessory might be presented several times with different currencies
var accessories = [
  {
      name: 'Accessory-1',
      currency: 'eur',
      price: 3,
      unit: 'm',
  },
  {
      name: 'Accessory-2',
      currency: 'eur',
      price: 5,
      unit: 'm',
  },
  {
      name: 'Accessory-3',
      currency: 'usd',
      price: 2,
      unit: 'pcs',
  },  
]

// Products - each product has properties with "accessories" inside
var products = [
  {
      name: 'Product',
      properties:
      [
          {
              no: 'Product-1',
              length: '2000',
              accessories: [
                      {
                      name: 'Accessory-1', 
                      qty: 2
                      },
                      {
                      name: 'Accessory-2', 
                      qty: 2
                      },
                      {
                        name: 'Accessory-3', 
                        qty: 1
                        },
                    ]
          },
      ]
  }
];

//currencies
let eurusd = 1.18;
let gbpusd = 1.38;
//functions
function getPrice(product) {
  return (
      product.currency === 'eur' ? (product.price * eurusd) : 
          (product.currency === 'gbp' ? (product.price * gbpusd)     : 
              product.price)).toFixed(2);
}

// The function gets one product as an incoming parameter
// Then gets the list of all accessories for all properties
// Finds the accessory from the global array, calculates right price
// based on accessory currency and multiplies it by accessory quantity
// Then summarises all prices
function getAccessories(product) {
  return product.properties
    .map(x => ({
      accessories: x.accessories.map(b => ({
        ...b, 
        length: +x.length
      }))
    }))
    .flatMap(x => x.accessories) // get all accessories
    .map(x => x.length * x.qty * getPrice(accessories.find(p => p.name === x.name))) // find the price of accessory from global array
    .reduce((c, p) => c + p) // sum prices
    ;
}
console.log(getAccessories(products[0]));

【讨论】:

  • 我不需要产品中的货币。我已经根据美元计算了它们的价格,但是附属货币是可变的,所以我只需要将平价乘以附属价格,除非它们是美元。
  • 好的。让我们尝试另一种方式。我们期望,每个配件在全局数组中只出现一次,我们需要使用系数计算实际价格。
  • 出于某种原因,我将查询拆分为 3 个部分 - 这样您就可以更轻松地根据需要进行调整。
  • 添加了长度乘法。不同的属性可能会有所不同,所以我们不能使用常量值
  • 您可能希望将答案标记为好答案 :-)
猜你喜欢
  • 2015-12-18
  • 2021-11-17
  • 2018-01-02
  • 1970-01-01
  • 2018-02-25
  • 2021-10-04
  • 1970-01-01
  • 1970-01-01
  • 2021-04-22
相关资源
最近更新 更多