locate index where a value is within the limit of two values of the array

Asked

Viewed 28 times

0

var current = 241;
var array = [ 
    { 
        start: 236.6,
        end: 239.42000000000002,
        value: 'hello word'
    },
    { 
        start: 239.42000000000002,
        end: 243.58,
        value: 'bar'
    },
    {
        start: 243.58,
        end: 246.12,
        value: 'X'
    }
]

The objective is to locate the index of the second array item where the variable current this among the values start and end array

1 answer

4


Your json is not formatted properly, there are syntax errors.

For it to work use:

var array =[ 
    { 
        start: 236.6,
        end: 239.42000000000002,
        value: 'hello word'
    },
    { 
        start: 239.42000000000002,
        end: 243.58,
        value: 'bar'
    },
    {
        start: 243.58,
        end: 246.12,
        value: 'X'
    }
]

Answering your question... To make a filter, Voce can use the following command: var resultado = array.filter(l => l.start <= current && l.end >= current)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.