поиск ближайшего элемента в массиве

Рейтинг: 0Ответов: 0Опубликовано: 10.04.2023

всем привет)б подскажите как решить этот таск у нас есть две кнопки - 1. добавляет секунды а 2. отнимает их и смысл в том чтоб найти ближайший элемент в массиве после того как добавил или отнял секунду к текущему

const findClosestTime = (currentTimestamp, arrayTimestamps, second) => {
        const lastTimestamp = arrayTimestamps[arrayTimestamps.length - 1]?.timestamp
        const firstTimestamp = arrayTimestamps[0]?.timestamp
        const time = new Date(currentTimestamp);

        time.setSeconds(time.getSeconds() + second);
        let result;

        const arrayTest = []

        

тут должен быть цикл что ищет ближайший элемент в массиве

        return arrayTimestamps.findIndex(({timestamp}) => timestamp === result);
    }


const plus15Second = () => {
        const timestampIndex = findClosestTime(inferences[currentFrame]?.timestamp, inferences, +30);
        if (currentFrame <= inferences.length) {
            changeCurrentStates(timestampIndex)
        }
    }
    const minus15Second = () => {
        const timestampIndex = findClosestTime(inferences[currentFrame]?.timestamp, inferences, -30);
        if (currentFrame !== 0) {
            changeCurrentStates(timestampIndex)
           
        }
    }

Ответы

Ответов пока нет.