Reputation: 129
I am trying to a pull array item number from a specific array.
for example if the input value has a number between 2.59 and 2.79 like 2.65
the output using this function
const gpaToLevel = [].concat(...
[1.99, 2.19, 2.39, 2.59, 2.79, 2.99, 3.19, 3.39, 3.59, 3.79, 4.00].map((ending, j, endValue) =>
Array(ending - (endValue[j-1] || -1)).fill(j)
)
);
should be 4, because 2.65 is less than 2.79 and 2.79 has a value of [4].
thank you
Upvotes: 2
Views: 79
Reputation: 92440
I think what you are looking for is findIndex()
. It takes a callback and will return the index of the first item where the callback returns true
.
So if your array is sorted you can use:
arr = [1.99, 2.19, 2.39, 2.59, 2.79, 2.99, 3.19, 3.39, 3.59, 3.79, 4.00]
// 4
console.log(arr.findIndex(item => item > 2.65))
// 5
console.log(arr.findIndex(item => item > 2.85))
// 0
console.log(arr.findIndex(item => item > 1.85))
// -1 -- no items greater than 4.0
console.log(arr.findIndex(item => item > 4.5))
Upvotes: 2