【问题标题】:Find elements from one array in another one and Sum the Total Price从另一个数组中的一个数组中查找元素并将总价格求和
【发布时间】:2021-04-08 13:02:31
【问题描述】:

我正在尝试通过在“对象:价格”中查找礼物的价格来计算给定名称(在本例中为“彼得”)中“对象:礼物”中礼物数组的“总成本”。如果present 不在 Prices 数组中,价格为 0。

ie:在彼得的情况下,礼物的“总成本”是 1,001,而在 Dana 的情况下是 6,800。

结果将是一个价格数组,给定 2 个对象数组中的匹配礼物(即 peter = [1,1000],因为他的礼物是“咖啡”和“假期”)所以稍后我计算总和里面有我的减速器功能。

我尝试通过过滤名称来获取数组,然后尝试查找包含的元素,但我的解决方案仅迭代第一个元素(咖啡)并返回一个未定义的数组。

关于为什么和如何前进的提示有什么想法吗?

我是一名初学者,非常感谢一些指导。

干杯,

const presents = [
  { "name": "peter", "presents": ["coffee", "holidays"], "present": "sock", "score": 10 },
  { "name": "dana", "presents": ["car", "phone"], "present": "sock", "score": 9.25 }
]

const prices = [{"present": "coffee","price": 1}, {"present": "holidays","price": 1000
  },{"present": "videogames","price": 40},{"present": "computer","price": 600},{"present": "tattoo","price": 30},{"present": "clothes","price": 80},{"present": "car","price": 6000},{"present": "phone","price": 800},{"present": "motorbike","price": 3500}]

const name = 'peter'
const presentsPrices = []
const filteredName = presents.filter((element)=>element.name===name?element:false).map((element, i)=> element.presents[i].includes(prices[i].present)? presentsPrices.push(prices[i].price):0)
const reducer = (accumulator, currentValue) => accumulator + currentValue;
const sum = presentsPrices.reduce(reducer)

console.log(sum)

【问题讨论】:

    标签: javascript arrays object filter


    【解决方案1】:

    您可以使用.find 获取礼物的价格。此外,不需要.filter.map,因为您正在搜索一个对象,然后获取它的presents(如果存在):

    const presents = [
      { "name": "peter", "presents": ["coffee", "holidays"], "present": "sock", "score": 10 },
      { "name": "dana", "presents": ["car", "phone"], "present": "sock", "score": 9.25 }
    ];
    const prices = [
      { "present": "coffee","price": 1 }, 
      { "present": "holidays","price": 1000 },
      { "present": "videogames","price": 40},
      { "present": "computer","price": 600 },   
      { "present": "tattoo","price": 30 },
      { "present": "clothes","price": 80 },
      { "present": "car","price": 6000},    
      { "present": "phone","price": 800 },
      { "present": "motorbike","price": 3500 }
    ];
    const name = 'peter';
    
    // get presents of person with this name
    const { presents: filteredNamePresents = [] } = presents.find(element => element.name===name) || {};
    console.log(`Presents: ${filteredNamePresents}`);
    
    // get prices list of these presents
    const presentsPrices = filteredNamePresents.reduce((acc,item) => {
      const { price } = prices.find(price => price.present===item) || {};
      acc = price ? [...acc, price] : acc;
      return acc;
    }, []);
    console.log(`Prices: ${presentsPrices}`);
    
    // calculate totla sum of the prices
    const sum = presentsPrices.reduce((accumulator, currentValue) => accumulator + currentValue);
    console.log(`Sum: ${sum}`);

    【讨论】:

      【解决方案2】:

      这里有一些东西可以帮助你的代码:

      const people = {
        "peter": ["coffee", "holidays"],
        ...
      };
      
      const presents = {
        "coffee": 1,
        "holidays": 1000,
        ...
      };
      

      但除了使用 JSON 更好地组织之外,我真的无法帮助您解决问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多