我对JS很陌生,也是第一次来这里.
如何在array.reduce中返回对象?
例如:
const lowestLow = historialCandlesFloatData.reduce((lowestLow,currentDay) => {
if(currentDay.l < lowestLow.l){
return lowestLow = currentDay;
}else return lowestLow;
},);
这很好,但我还需要返回最低的对象索引.现在我知道你可以使用索引作为reduce的一部分,我可以控制台.记录最低低点是什么索引,但我不知道如何将其与低点一起作为对象返回?
我可以用一个forEach,它看起来很好,但这似乎不是最好的方法,我真的很想知道!
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,
}
}