使用过滤器和减少 JS
Using filter and reduce JS
我正在尝试使用 filter 和 reduce 来获取 Bob 购买的总价,以对我正在执行的高阶函数进行一些练习。
我给
const purchases = [{"owner":"Barry","price":103},{"owner":"Bob","price":75},
{"owner":"Bob","price":73},{"owner":"Barry","price":57},{"owner":"Barry","price":128},
{"owner":"Bob","price":119},{"owner":"Barry","price":133},{"owner":"Barry","price":27},
{"owner":"Barry","price":138},{"owner":"Bob","price":68},{"owner":"Bob","price":50},
{"owner":"Barry","price":9},{"owner":"Bob","price":123},{"owner":"Bob","price":135},
{"owner":"Barry","price":30},{"owner":"Barry","price":129},{"owner":"Barry","price":38},
{"owner":"Bob","price":133},{"owner":"Barry","price":109},{"owner":"Bob","price":115}]
使用高阶方法创建得到bobsTotal的和
这就是我的想法。我可以将鲍勃的购买过滤到一个数组中,但我现在无法获得总价。为了日志目的,我暂时将我的 reduce 部分注释掉了。
let bobsTotal = purchases.filter(total=>total=purchases.owner="Bob")//.reduce((total, price) => total + price)
console.log(bobsTotal)
任何建议都很好,或者任何其他方法都很好。
你非常接近。
filter
的回调需要是 returns true
或 false
.
的函数
reduce
的回调参数是 aggregate
(因为您将列表聚合成一个东西)和 current value
,您需要设置默认值 0
, 有东西可以总结。
const purchases = [{"owner":"Barry","price":103},{"owner":"Bob","price":75},
{"owner":"Bob","price":73},{"owner":"Barry","price":57},{"owner":"Barry","price":128},
{"owner":"Bob","price":119},{"owner":"Barry","price":133},{"owner":"Barry","price":27},
{"owner":"Barry","price":138},{"owner":"Bob","price":68},{"owner":"Bob","price":50},
{"owner":"Barry","price":9},{"owner":"Bob","price":123},{"owner":"Bob","price":135},
{"owner":"Barry","price":30},{"owner":"Barry","price":129},{"owner":"Barry","price":38},
{"owner":"Bob","price":133},{"owner":"Barry","price":109},{"owner":"Bob","price":115}]
let bobtot = purchases.filter(p => p.owner === 'Bob').reduce((sum, p) => sum+p.price, 0);
console.log('bob\'s total:', bobtot);
我正在尝试使用 filter 和 reduce 来获取 Bob 购买的总价,以对我正在执行的高阶函数进行一些练习。 我给
const purchases = [{"owner":"Barry","price":103},{"owner":"Bob","price":75},
{"owner":"Bob","price":73},{"owner":"Barry","price":57},{"owner":"Barry","price":128},
{"owner":"Bob","price":119},{"owner":"Barry","price":133},{"owner":"Barry","price":27},
{"owner":"Barry","price":138},{"owner":"Bob","price":68},{"owner":"Bob","price":50},
{"owner":"Barry","price":9},{"owner":"Bob","price":123},{"owner":"Bob","price":135},
{"owner":"Barry","price":30},{"owner":"Barry","price":129},{"owner":"Barry","price":38},
{"owner":"Bob","price":133},{"owner":"Barry","price":109},{"owner":"Bob","price":115}]
使用高阶方法创建得到bobsTotal的和
这就是我的想法。我可以将鲍勃的购买过滤到一个数组中,但我现在无法获得总价。为了日志目的,我暂时将我的 reduce 部分注释掉了。
let bobsTotal = purchases.filter(total=>total=purchases.owner="Bob")//.reduce((total, price) => total + price)
console.log(bobsTotal)
任何建议都很好,或者任何其他方法都很好。
你非常接近。
filter
的回调需要是 returns true
或 false
.
reduce
的回调参数是 aggregate
(因为您将列表聚合成一个东西)和 current value
,您需要设置默认值 0
, 有东西可以总结。
const purchases = [{"owner":"Barry","price":103},{"owner":"Bob","price":75},
{"owner":"Bob","price":73},{"owner":"Barry","price":57},{"owner":"Barry","price":128},
{"owner":"Bob","price":119},{"owner":"Barry","price":133},{"owner":"Barry","price":27},
{"owner":"Barry","price":138},{"owner":"Bob","price":68},{"owner":"Bob","price":50},
{"owner":"Barry","price":9},{"owner":"Bob","price":123},{"owner":"Bob","price":135},
{"owner":"Barry","price":30},{"owner":"Barry","price":129},{"owner":"Barry","price":38},
{"owner":"Bob","price":133},{"owner":"Barry","price":109},{"owner":"Bob","price":115}]
let bobtot = purchases.filter(p => p.owner === 'Bob').reduce((sum, p) => sum+p.price, 0);
console.log('bob\'s total:', bobtot);