[英]How to .filter and .reduce javascript array object
WebDev Student. WebDev学生。 I am having an issue with using
.filter + .reduce
array methods and chaining to return the total cost of both items. 我在使用
.filter + .reduce
数组方法和链接返回两个项目的总成本时遇到问题。 Codeburst.io on the subject is super helpful but I need direction on how to apply to this specific exercise as I am targeting a property: value in the array. 关于该主题的Codeburst.io超级有帮助,但是由于我要针对属性:数组中的值,因此我需要有关如何应用于此特定练习的指导。
This is an exercise in reducing the amount of code., so rather than using a for
loop to iterate through an array, I need to apply a method(s) to .filter through and .reduce to return the total cost of each item in the array. 这是减少代码量的一种练习。因此,与使用
for
循环遍历数组相比,我需要对.filter through和.reduce应用一个或多个方法来返回其中每个项目的总成本。数组。 .price
Using the
shopCart
variable, create a function that takes theshopCart
variable and returns the total cost of both items as thetotal
variable.使用
shopCart
变量,创建一个使用shopCart
变量并将两个项目的总成本作为total
变量返回的函数。 Add Code after "// code below".在“ //下面的代码”之后添加代码。
var shopCart = [ { id: 0, name: 'Womens Shirt', price: 30, size: 'Small' }, { id: 1, name: 'childs shirt', price: 12, size: 'Large' } ] function getCost(items){ let total = 0; // code below // code above return total; } getCost(shopCart) < --- Add let cost = getCost(shopCart); < ---OMIT console.log(cost); < -- OMIT
PLease re-review - Code has been amended. 请重新审核-代码已修改。
Here's a more explicit approach. 这是一种更明确的方法。
function sum(nums) {
return nums.reduce((a, b) => a + b)
}
const prices = items.map(item => item.price)
const total = sum(prices)
You can do this using iteration – for example, a for loop. 您可以使用迭代来执行此操作,例如for循环。 In the
getCost
function, we have a total variable with an initial value of zero. 在
getCost
函数中,我们有一个初始值为零的总变量。 Then for each element in the items array, the price is used to accumulate the total. 然后,对于items数组中的每个元素,将使用价格来累计总数。 However, this functionality is similarly available using the reduce method .
但是,使用reduce方法类似地可以使用此功能。
var shopCart = [ { id: 0, name: 'Womens Shirt', price: 30, size: 'Small' }, { id: 1, name: 'childs shirt', price: 12, size: 'Large' } ]; function getCost(items) { var total = 0; items.forEach(e => total += e.price); return total; } let cost = getCost(shopCart); // 42 let costWithReduce = shopCart.reduce((a, c) => a + c.price, 0); // 42 console.log(cost, costWithReduce);
You can use array#reduce
. 您可以使用
array#reduce
。 In a total
accumulator, you can add price
of each object. 在
total
,您可以添加每个对象的price
。
var shopCart = [{id: 0,name: 'Womens Shirt',price: 30,size: 'Small'},{id: 1,name: 'childs shirt',price: 12,size: 'Large'}]; function getCost(items){ return items.reduce(function(total, obj){ return total + obj.price; }, 0); } let cost = getCost(shopCart); console.log(cost);
var shopCart = [{id: 0,name: 'Womens Shirt',price: 30,size: 'Small'},{id: 1,name: 'childs shirt',price: 12,size: 'Large'}]; function getCost(items){ return items.reduce((total, {price}) => total + price, 0); } let cost = getCost(shopCart); console.log(cost);
Answer same as others really, but within the required parameters. 确实与其他人回答相同,但在必填参数之内。
var shopCart = [ { id: 0, name: 'Womens Shirt', price: 30, size: 'Small' }, { id: 1, name: 'childs shirt', price: 12, size: 'Large' } ] function getCost(items){ let total = 0; // code below total = items.reduce((sum, item) => sum + item.price, total); // code above return total; } let cost = getCost(shopCart); console.log(cost);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.