How to reduce the following array down to a single number?

Jon Nicholson

I have the following variable:

const currentMonthCreditArray = creditCards.flatMap(x => x.balance.filter(y => (new Date(y.balanceDate).getMonth() === new Date().getMonth())))

Which outputs the following when console logged:

{_id: "5fc4aa02959b1409f2edab69", newBalance: 300, balanceDate: "2020-11-30T08:14:58.035Z"}, {_id: "5fc51cb0bd4d9a0f6059bdbe", newBalance: 400, balanceDate: "2020-11-30T16:24:14.390Z"}]

What I want to do is take each of the 'newBalance' entries from the currentMonthCreditArray and reduce them down (i.e. add them together). However I can't figure out how to do it.

I have tried the following, which errors out:

const currentMonthFinal = currentMonthCreditArray.flatMap(x => x.balance.map(y => y.newBalance.reduce((a,b) => a + b, 0)))

Can anyone point out how to reduce my array into a variable - hopefully a simple one.. Thanks!

Montgomery Watts
const currentMonthFinal = currentMonthCreditArray.reduce((acc, current) => acc += current.newBalance, 0)

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

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

編集
0

コメントを追加

0

関連記事

分類Dev

Kotlin: How can I reduce child arrays into a single array?

分類Dev

Randomize and then Reduce Array down to one value

分類Dev

reduce a parallel stream of arrays into a single array

分類Dev

How to reduce execution time in C++ for the following code?

分類Dev

How to reduce number of requests dojo makes to the server?

分類Dev

How to reduce the number of items in the TFS workspace

分類Dev

How to reduce the number of repetitions for formatting many tables?

分類Dev

How to reduce textsize of a number (to display entire number) in a textview as it gets longer?

分類Dev

How to print array data following current date

分類Dev

How should I handle component state following single responsibility pattern

分類Dev

How to reduce two array to get a custom object

分類Dev

How to "number" array items?

分類Dev

How to down sample an array in python without a for loop

分類Dev

How to reduce the number of fields requested during registration? | OpenCart 2

分類Dev

How can I reduce number of trys left in a game?

分類Dev

How to convert the following JSON array into IDictionary<string, object>?

分類Dev

How do I reshape the following array into the other one?

分類Dev

How to store a number array in MongoDB

分類Dev

How to simplify javascript nested array with map(), reduce(), and filter()?

分類Dev

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

分類Dev

TypeScript how to set types for accumulated Value and initialValue on Reduce function on an array

分類Dev

Get n number of points from angle 0 to 2π using an array operator like map or reduce?

分類Dev

How to group multiple columns into a single array or similar?

分類Dev

How to display json array values in single UITableVIewCell

分類Dev

How to add array items in one single item?

分類Dev

C ++ std :: reduce with array

分類Dev

Reduce number of files served by Vue

分類Dev

Reduce the number of drawn TChart points

分類Dev

How to Update a Single record despite multiple Occurances of the same ID Number?

Related 関連記事

  1. 1

    Kotlin: How can I reduce child arrays into a single array?

  2. 2

    Randomize and then Reduce Array down to one value

  3. 3

    reduce a parallel stream of arrays into a single array

  4. 4

    How to reduce execution time in C++ for the following code?

  5. 5

    How to reduce number of requests dojo makes to the server?

  6. 6

    How to reduce the number of items in the TFS workspace

  7. 7

    How to reduce the number of repetitions for formatting many tables?

  8. 8

    How to reduce textsize of a number (to display entire number) in a textview as it gets longer?

  9. 9

    How to print array data following current date

  10. 10

    How should I handle component state following single responsibility pattern

  11. 11

    How to reduce two array to get a custom object

  12. 12

    How to "number" array items?

  13. 13

    How to down sample an array in python without a for loop

  14. 14

    How to reduce the number of fields requested during registration? | OpenCart 2

  15. 15

    How can I reduce number of trys left in a game?

  16. 16

    How to convert the following JSON array into IDictionary<string, object>?

  17. 17

    How do I reshape the following array into the other one?

  18. 18

    How to store a number array in MongoDB

  19. 19

    How to simplify javascript nested array with map(), reduce(), and filter()?

  20. 20

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

  21. 21

    TypeScript how to set types for accumulated Value and initialValue on Reduce function on an array

  22. 22

    Get n number of points from angle 0 to 2π using an array operator like map or reduce?

  23. 23

    How to group multiple columns into a single array or similar?

  24. 24

    How to display json array values in single UITableVIewCell

  25. 25

    How to add array items in one single item?

  26. 26

    C ++ std :: reduce with array

  27. 27

    Reduce number of files served by Vue

  28. 28

    Reduce the number of drawn TChart points

  29. 29

    How to Update a Single record despite multiple Occurances of the same ID Number?

ホットタグ

アーカイブ