I'm pretty new to JS and first time on here.
How do you return an object within an array.reduce?
So for example: I want to find the lowest low for a given day's dataset (I'm using a currency API as an interesting data-set to play with). The data I get back is an array of objects. So I was using a reduce to iterate over, day-by-day and find the lowest low.
const lowestLow = historialCandlesFloatData.reduce((lowestLow,currentDay) => {
if(currentDay.l < lowestLow.l){
return lowestLow = currentDay;
}else return lowestLow;
},);
That works fine, but I'm also going to need the index of the object returned as the lowest. Now I know you can use index as part of reduce, and I can console.log what index the lowest low is, but I can't figure out how to return it along with the low as an object?
I could just use a forEach, which seems to work fine, but this doesn't seem the best way to do it and id really like to know!
const findLow = ()=>{
let currentDayLow = 2;
let dataIndex = 0;
let datavalue = 0;
historialCandlesFloatData.forEach((day)=>{
if(day.l < currentDayLow){
currentDayLow = day.l;
datavalue = dataIndex;
}
dataIndex ++;
});
return {
lowestLow: currentDayLow,
indexValue: datavalue,
}
}