Sum values of objects which are inside an array with underscore.js and reduce

kirqe

I'm trying to sum values of objects inside and array with underscore.js and its reduce method. But it looks like I'm doing something wrong. Where's my problem?

let list = [{ title: 'one', time: 75 },
        { title: 'two', time: 200 },
        { title: 'three', time: 500 }]

let sum = _.reduce(list, (f, s) => {
    console.log(f.time); // this logs 75
    f.time + s.time
})

console.log(sum); // Cannot read property 'time' of undefined
ibrahim mahrir

Use the native reduce since list is already an array.

reduce callback should return something, and have an initial value.

Try this:

let list = [{ title: 'one', time: 75 },
        { title: 'two', time: 200 },
        { title: 'three', time: 500 }];

let sum = list.reduce((s, f) => {
    return s + f.time;               // return the sum of the accumulator and the current time, as the the new accumulator
}, 0);                               // initial value of 0

console.log(sum);

Note: That reduce call can be shortened even more if we omit the block and use the implicit return of the arrow function:

let sum = list.reduce((s, f) => s + f.time, 0);

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Underscore JS difference on array and array with objects

分類Dev

Reduce the array of object by object key and sum the values

分類Dev

filter array of objects with underscore and get new array of values

分類Dev

JS reduce - sum only array lengths in object

分類Dev

Sum of values inside an array if identifier is identical

分類Dev

How to get all values of objects inside array

分類Dev

How to use reduce to retrieve values from deep nested array objects

分類Dev

Sum and average values of multiple objects in an array into one array

分類Dev

How to find sum objects inside an array using mongoose ,node ,

分類Dev

How to find sum objects inside an array using mongoose ,node ,

分類Dev

Get union of keys of all objects in js array using reduce

分類Dev

How to get the sum of number from an array of objects, using pure JS?

分類Dev

Re-structuring an array of objects with Underscore

分類Dev

underscore - convert complex object into array using underscore.js

分類Dev

Reduce an array of objects based on object field

分類Dev

Match value in array, that is inside an array of objects

分類Dev

Selecting values inside array in jsonb

分類Dev

Replace array values inside a document

分類Dev

Query Mongodb Sum of firsts element of an array of objects

分類Dev

Using reduce to add values of properties of a collection objects in JavaScript

分類Dev

Find the value inside the array of objects that include children from the array of objects

分類Dev

Underscore.js _.reduceメソッドを再作成する方法は?

分類Dev

Extract child objects into array inside MongoDB aggregation

分類Dev

Finding maximum and minimum Inside array of objects

分類Dev

dc.js reduce space/padding inside svg element

分類Dev

Counting occurrence of values in an array of objects in another array of objects

分類Dev

Angular - filter an array of objects based on values in another array of objects

分類Dev

Add/sum multiple values in array with same id

分類Dev

JS find() not working on array of objects

Related 関連記事

  1. 1

    Underscore JS difference on array and array with objects

  2. 2

    Reduce the array of object by object key and sum the values

  3. 3

    filter array of objects with underscore and get new array of values

  4. 4

    JS reduce - sum only array lengths in object

  5. 5

    Sum of values inside an array if identifier is identical

  6. 6

    How to get all values of objects inside array

  7. 7

    How to use reduce to retrieve values from deep nested array objects

  8. 8

    Sum and average values of multiple objects in an array into one array

  9. 9

    How to find sum objects inside an array using mongoose ,node ,

  10. 10

    How to find sum objects inside an array using mongoose ,node ,

  11. 11

    Get union of keys of all objects in js array using reduce

  12. 12

    How to get the sum of number from an array of objects, using pure JS?

  13. 13

    Re-structuring an array of objects with Underscore

  14. 14

    underscore - convert complex object into array using underscore.js

  15. 15

    Reduce an array of objects based on object field

  16. 16

    Match value in array, that is inside an array of objects

  17. 17

    Selecting values inside array in jsonb

  18. 18

    Replace array values inside a document

  19. 19

    Query Mongodb Sum of firsts element of an array of objects

  20. 20

    Using reduce to add values of properties of a collection objects in JavaScript

  21. 21

    Find the value inside the array of objects that include children from the array of objects

  22. 22

    Underscore.js _.reduceメソッドを再作成する方法は?

  23. 23

    Extract child objects into array inside MongoDB aggregation

  24. 24

    Finding maximum and minimum Inside array of objects

  25. 25

    dc.js reduce space/padding inside svg element

  26. 26

    Counting occurrence of values in an array of objects in another array of objects

  27. 27

    Angular - filter an array of objects based on values in another array of objects

  28. 28

    Add/sum multiple values in array with same id

  29. 29

    JS find() not working on array of objects

ホットタグ

アーカイブ