如何减少一系列的对象的弦乐的价格

0

的问题

var groceries = [
  {
    id: 1,
    product: 'Olive Oil',
    price: '$' + 12.1
  },
  {
    id: 2,
    product: 'Tomato Soup',
    price: '$' + 3.48
  },
  {
    id: 3,
    product: 'Cheesecake',
    price: '$' + 17.36
  },
  {
    id: 4,
    product: 'Sirloin Steak',
    price: '$' + 14.8
  },
  {
    id: 5,
    product: 'Brie Cheese',
    price: '$' + 23.28
  }
];

var sum = _.reduce(products, function (total, price) {
    return total + price;
  }, 0);

我不太确定如何删除'$'从价格之前,我们开始增加价值。 我已经试过了我最好的寻找其他解决方案的在这里(我是新的),但是那里似乎只是例子,"价格"仅仅是数字。

对不起,如果这一类似的问题已经张贴在其他地方,但我仍然在学习如何在这里,我还没有找到类似的情况,除非有人可以指我了!

integer javascript reduce string
2021-11-23 08:38:50
2

最好的答案

0

在这里,我已经使用Javascript是默认的功能 reduce 为获得累积的总和。

var groceries = [
  {
    id: 1,
    product: 'Olive Oil',
    price: '$' + 12.1
  },
  {
    id: 2,
    product: 'Tomato Soup',
    price: '$' + 3.48
  },
  {
    id: 3,
    product: 'Cheesecake',
    price: '$' + 17.36
  },
  {
    id: 4,
    product: 'Sirloin Steak',
    price: '$' + 14.8
  },
  {
    id: 5,
    product: 'Brie Cheese',
    price: '$' + 23.28
  }
];

//reduce((total, currentIteratedValue) => {}, initialCumulativeValue)
//Initially we take sum as 0
const sum = groceries.reduce(function (currentTotal, obj) {
  var price = parseFloat(obj.price.slice(1));
  if (!isNaN(price)) return currentTotal + price;  
  return currentTotal;
}, 0);


console.log(sum)

2021-11-23 09:52:50

这工作! 谢谢你这么多。 我被误解的迭代部分..
kon
0

在代码, price 你目前使用的对象的每一次迭代,与的性质从阵列。 相反,可以采取价格的财产的对象。

在你的数据,只有领先 $ 你可以从价格的财产。 然后你就可以使用例如需要被和只增加的价值,如果转换不产生楠。

然后通过 groceries 变量减少而不是的 products 这是不存在例码。

注意,目前我们正在增加的价值的同一货币,而如果你有不同的货币你有必须考虑,在计算总和。

var groceries=[{id:1,product:'Olive Oil',price:'$'+12.1},{id:2,product:'Tomato Soup',price:'$'+3.48},{id:3,product:'Cheesecake',price:'$'+17.36},{id:4,product:'Sirloin Steak',price:'$'+14.8},{id:5,product:'Brie Cheese',price:'$'+23.28},{id:6,product:'Product with invalid price',price:'$'+"hello"}];

var sum = _.reduce(groceries, function (total, obj) {
  var price = parseFloat(obj.price.replace(/^\$/, ''));
  if (!isNaN(price)) {
    return total + price;  
  }
  return total;
}, 0);

console.log(sum)
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"></script>

2021-11-23 09:38:30

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................