我想在从零以外的元素开始的数组上使用findIndex()。我已经写了下面的代码,对我有用,但是有更干净的解决方案吗?

let desiredLastElementIndex = refundRequests.map((e,i) =>{
    if(i<numberOfTickets){
        return 0
    }else{
        return Math.round(e.purchasersMinimumPrice - refundRequests[i-1].purchasersMinimumPrice)
        }
        }).findIndex((e, i) => findIndex((e, i) => e !== 0)


例如,当使用indexOf时,我可以将要从其开始的索引作为第二个参数传递。

理想情况下,我想做类似...

    let desiredLastElementIndex = refundRequests.findIndex((e, i) => {
     e.purchasersMinimumPrice !== refundRequests[i+1].purchasersMinimumPrice
}, INDEXTOSTARTFROM) + 1

最佳答案

我会做

  let desiredLastElementIndex = refundRequests.findIndex((e, i) => i >= numberOfTickets && e.purchasersMinimumPrice !== refundRequests[i+1].purchasersMinimumPrice);

10-06 00:50