简体   繁体   English

如何返回总数的数组 - 减少 function - Javascript

[英]How to return an array of the total - reduce function - Javascript

I have this array of movies, I'm trying to use the reduce function and the expected output should be: 420(the total duration of the 3 objects), the output I'm getting right now is like a concatenation of the 3 strings, why is that? I have this array of movies, I'm trying to use the reduce function and the expected output should be: 420(the total duration of the 3 objects), the output I'm getting right now is like a concatenation of the 3 strings , 这是为什么?

 let movies = [ { 'name': 'Jumanji', 'duration': 120 }, { 'name': 'Harry Potter', 'duration': 60 }, { 'name': 'Toy Story', 'duration': 240 } ]; let newAr = movies.reduce((previousValue, currentValue) => [ previousValue + currentValue.duration ], []); console.log('newAr', newAr)

The previousValue will be the accumulator, the value returned by the prior iteration of the callback - or, the initial value - so it'll be an array. previousValue将是累加器,回调的先前迭代返回的值 - 或者,初始值 - 所以它将是一个数组。 Doing someArray + someOtherValue will coerce the array into a string (in other words, doing .join(',') ) and concatenate it with the expression on the right.执行someArray + someOtherValue会将数组强制转换为字符串(换句话说,执行.join(',') )并将其与右侧的表达式连接起来。

I'd map the movies to their durations instead, then add them up:我将 map 电影改为它们的持续时间,然后将它们相加:

const totalDuration = movies
  .map(m => m.duration)
  .reduce((a, b) => a + b, 0);

Or do it in one step, though it might be a bit harder to parse at a glance if you're unfamiliar with reduce .或者一步完成,但如果您不熟悉reduce ,可能会难以一目了然地解析。

const totalDuration = movies
  .reduce((a, movie) => a + movie.duration, 0);

 const movies = [ { 'name': 'Jumanji', 'duration': 120 }, { 'name': 'Harry Potter', 'duration': 60 }, { 'name': 'Toy Story', 'duration': 240 } ]; console.log(movies.reduce((previousValue, currentValue) => previousValue + currentValue.duration, 0))

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

相关问题 JavaScript:如何从 Array.reduce() 函数返回映射数组? - JavaScript: How to return mapped array from Array.reduce() function? 如何使用数字数组使用Java中的reduce函数返回最高和最低数字 - How do I take an array of numbers and return both the highest and the lowest number using the reduce function in Javascript 使用reduce函数返回一个数组 - Using the reduce function to return an array 如何使用Javascript / TypeScript减少总和 - How to calculate total using Javascript/ TypeScript reduce 使用 JavaScript 减少函数对数组进行排序 - Sorting Array with JavaScript reduce function 在函数数组上执行Javascript'reduce'如何实现函数组合? - How Javascript `reduce` performed on function array achieves function composition? 如何通过使用reduce函数有效地评估javascript数组中的元素 - How to efficiently evaluate element in javascript array by using reduce function 如何对数组中的对象使用javascript reduce函数获取字符串值 - How to use javascript reduce function for objects in an array to get string values Javascript数组减少返回零而不是未定义 - Javascript array reduce return zero instead of undefined Javascript:json对象数组的reduce函数不返回值的相加 - Javascript: reduce function of an array of json objects don't return the addition of values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM