简体   繁体   English

如何返回该对象数组的总数?

[英]How do I return the total of this object array?

I need to take the price and tax of these and return the total of everything. 我需要承担这些的价格和税金,并退还所有款项。 I'm learning so I apologize for the simple question. 我正在学习,因此我对这个简单的问题表示歉意。

const orders = [{"price":15,"tax":0.09},{"price":42,"tax":0.07},{"price":56,"tax":0.11},
{"price":80,"tax":0.11},{"price":69,"tax":0.06},{"price":68,"tax":0.14},
{"price":72,"tax":0.14},{"price":51,"tax":0.09},{"price":89,"tax":0.15},
{"price":48,"tax":0.13}];
// Do not edit code above.

/*
  Use a higher order method to get the sum of all the order totals after adding in the sales tax
*/

var ordersTotal  = orders.reduce(function(total, num) {
    return total + num;
    })
  ordersTotal;

You need to give reduce something to start with, in this example 0 is probably a good start. 您需要给reduce开始一些东西,在这个例子中0可能是一个好的开始。 Then each num passed to reduce will be an object. 然后,传递给reduce每个num都是一个对象。 Currently you're just adding the object like total = total + {"price":15,"tax":0.09} and that doesn't work. 当前,您只是添加了诸如total = total + {"price":15,"tax":0.09} ,因此无法正常工作。 You need to look at each property you want to add. 您需要查看要添加的每个属性。 It's not clear if tax is a percentage or a total amount. 目前尚不清楚税收是百分比还是总额。 Below we'll just add it, but it should be clear how to add as a percentage if you want. 在下面,我们将仅添加它,但是如果需要,应该清楚如何添加百分比。

 const orders = [{"price":15,"tax":0.09},{"price":42,"tax":0.07},{"price":56,"tax":0.11},{"price":80,"tax":0.11},{"price":69,"tax":0.06},{"price":68,"tax":0.14},{"price":72,"tax":0.14},{"price":51,"tax":0.09},{"price":89,"tax":0.15},{"price":48,"tax":0.13}]; var ordersTotal = orders.reduce(function(total, num) { return total + num.price + num.tax; // add properties }, 0) // start with 0 console.log(ordersTotal); 

Simply use Array.reduce() and Object destructing . 只需使用Array.reduce()Object destructing即可 And please make sure to pass 0 as the initial value to your reduce function. 并且请确保将0作为初始值传递给reduce函数。

 const orders = [{"price":15,"tax":0.09},{"price":42,"tax":0.07},{"price":56,"tax":0.11},{"price":80,"tax":0.11},{"price":69,"tax":0.06},{"price":68,"tax":0.14},{"price":72,"tax":0.14},{"price":51,"tax":0.09},{"price":89,"tax":0.15},{"price":48,"tax":0.13}]; const result = orders.reduce((a,{price,tax})=>a+price+tax,0); console.log(result); 

Make sure you start with a zero so it doesn't try to coerce the result into a string. 确保以零开头,这样就不会尝试将结果强制为字符串。

 const orders = [{"price":15,"tax":0.09},{"price":42,"tax":0.07},{"price":56,"tax":0.11}, {"price":80,"tax":0.11},{"price":69,"tax":0.06},{"price":68,"tax":0.14}, {"price":72,"tax":0.14},{"price":51,"tax":0.09},{"price":89,"tax":0.15}, {"price":48,"tax":0.13}]; // Do not edit code above. var ordersTotal = orders.reduce(function(total, order) { return total + order.price + order.tax; },0) console.log(ordersTotal,ordersTotal.toFixed(2)) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何`.filter()`一个数组/对象并返回一个带有原始键的新数组,而不是作为一个索引数组/对象作为过滤器返回? - How do I `.filter()` an array/object and return a new array with the original keys and not as an indexed array/object as filter return? 如何制定查询以返回对象中内部数组的元素? - How do I formulate a query to return the elements of an inner array in an Object? 如何遍历嵌套对象并返回数组中的值? - How do I Iterate through a nested object and return values in an array? 如何将ActiveX对象的字符串数组返回给JScript - How do I return an array of strings from an ActiveX object to JScript 如何返回数组中 object 的索引 -Javascript - How Do I return the index of an object in an array -Javascript 如何从对象中获取键和值并返回数组? - How do I get they keys and the values from the object and return an array? 如何获得object的总数组 - how to get the total array of object 我如何使用reduce来计算对象中一个值的总和 - How do I use reduce to calculate the total of a value in an object 如何在 Javascript 的数组中返回仅包含某个 object 的对象数组? - How do I return an array of objects that only contains a certain object in an array for Javascript? 如何使用 mongoose 中的用户数组 object id 返回文档数组? - How do I return an array of documents using an array of users object ids in mongoose?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM