Engr Umair
Engr Umair

Reputation: 130

How to Use Reduce function in JS

I have the following data in globalAskData array and mid price is 99328 and range of 500 so last value should be less than 99328 + 500 => 99828

99320, 99328, 99332, 99344

.

.

99432, 99455, 99464

.

.

99821, 99823, 99844

and expect output should be

99332, 99344, 99432 . . . 99823

Here is what I tried:

globalAskData = tempDataAsk.reduce(function(ask) {
                    if (ask.price < (mid_price + 500) && ask.price > mid_price) {
                        return ask;
                    }
                }, tempDataAsk[0])

Upvotes: 0

Views: 118

Answers (1)

Max Peng
Max Peng

Reputation: 3287

I think you need a filter instead of reduce.

Something like this:

globalAskData = tempDataAsk.filter(function(ask) {
                    return ask.price < (mid_price + 500) && ask.price > mid_price;
                })

Upvotes: 2

Related Questions