findIndex was added in 1.8:

index = _.findIndex(tv, function(voteItem) { return voteItem.id == voteID })

See: http://underscorejs.org/#findIndex

Alternatively, this also works, if you don't mind making another temporary list:

index = _.indexOf(_.pluck(tv, 'id'), voteId);

See: http://underscorejs.org/#pluck

Answer from Ropez on Stack Overflow
🌐
Lodash
lodash.com › docs
Lodash Documentation
Gets the last element of array. ... This method is like _.indexOf except that it iterates over elements of array from right to left.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › lodash-_-indexof-method
Lodash _.indexOf() Method - GeeksforGeeks
September 2, 2024 - lodash _.indexOf() method is used to get the index of the first occurrence of the particular element in the array.
🌐
Dustin John Pfister
dustinpfister.github.io › 2019 › 06 › 26 › lodash_indexof
loash _.indexOf | Dustin John Pfister at github pages
July 11, 2020 - The lodash indexOf method can be used as a way to get the first index that matches the value that is given as the second argument when called. There is however also the _.findIndex method as well that is a little more advanced as it can be used ...
🌐
Educative
educative.io › answers › what-is-the-findindex-method-in-lodash
What is the _.findIndex() method in Lodash?
The _.findIndex() method in Lodash is used to find the index of an element inside an array.
🌐
Dustin John Pfister
dustinpfister.github.io › 2018 › 02 › 09 › lodash-find-index
The lodash findIndex array method and vanilla javaScript alternatives | Dustin John Pfister at github pages
December 20, 2021 - One way to go about doing that would be to use the lodash find index method by passing the array of names as the first argument, followed by a function that will be used to find out if this is the index that I want or not.
🌐
GeeksforGeeks
geeksforgeeks.org › lodash-_-findindex-method
Lodash _.findIndex() Method | GeeksforGeeks
September 2, 2024 - It returns the index of the element if found else -1 is returned. Example 1: In this example, an element is searched starting from index 0 and printing the result according to the arrow function used in the _.findIndex() method.
🌐
Devsheet
devsheet.com › code-snippet › lodash-get-index-of-array-item-based-on-some-condition
Lodash - Get index of array item based on some condition - Devsheet
To find an index of an array item based on some condition, you can use _.findIndex of lodash. const users = [ { 'user': 'John Deo', 'active': false }, { 'user': 'Rick Grimes', 'active': false }, { 'user': 'Negan', 'active': true } ]; //Find ...
🌐
GitHub
github.com › jashkenas › underscore › issues › 557
_.indexOf doesn't work on arrays of objects · Issue #557 · jashkenas/underscore
indexOf: function (array, item, isSorted) { if (array == null) return -1; var i, l; if (isSorted) { i = _.sortedIndex(array, item); return array[i] === item ? i : -1; } if (nativeIndexOf && array.indexOf === nativeIndexOf) return array.indexOf(item); for (i = 0, l = array.length; i < l; i++) if (i in array && array[i] === item) return i; return -1; } New version uses an optional deepEquality flag. Deep equality test cannot use nativeIndexOf and is significantly slower. indexOf: function (array, item, isSorted, deepEquality) { var eqfn = deepEquality ?
Find elsewhere
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript find index of object in array
How to Find Index of Object in JavaScript Array | Delft Stack
February 2, 2024 - We can install it locally as npm i --save lodash or yarn add lodash. With everything all set up, we can use the _.findIndex() method. The syntax is shown below. _.findIndex(array, [predicate = _.identity], [fromIndex = 0]) Here, arguments array ...
🌐
Lodash
lodash.info › doc › indexOf
indexOf - Lodash documentation
Gets the index at which the first occurrence of value is found in array using SameValueZero for equality comparisons. If fromIndex is negative, it's used as the offset from the end of array.
🌐
TutorialsPoint
tutorialspoint.com › lodash › lodash_indexof.htm
Lodash - indexOf method
Gets the index at which the first occurrence of value is found in array using SameValueZero for equality comparisons. If fromIndex is negative, it's used as the offset from the end of array. ... var _ = require('lodash'); var list = [1, 2, 3, 2, 5, 2]; var result = _.indexOf(list,2) ...
🌐
TutorialsPoint
tutorialspoint.com › home › lodash › lodash findindex method
Lodash findIndex Method
January 20, 2021 - This method is like _.find except that it returns the index of the first element predicate returns truthy for instead of the element itself. ... var _ = require('lodash'); var users = [ { user: 'Sam', active: false }, { user: 'Ted', active: ...
Top answer
1 of 3
11

From the Lodash documentation:

Iterates over elements of collection and invokes iteratee for each element. The iteratee is invoked with three arguments: (value, index|key, collection). Iteratee functions may exit iteration early by explicitly returning false.

This means that you can simply do:

_.each( days, function( day, i ){

});

So your whole code becomes:

var days = [5, 15, 25];
var hours = [6, 8, 7];
var result = [];

_.each( days, function( day, i ){
    if( days[i] < a && b < days[i+1] ){ // days[i] == day
        result.push( hours[i] );
    } else if( days[i] > a && days[i] < b ){
        result.push( hours[i] );
        if( i > 0 ){
            result.push( hours[i-1] );
        }
    }
})

Here is a jsFiddle to experiment.

2 of 3
1

From Lodash Documentation:

Iterates over elements of collection and invokes iteratee for each element. The iteratee is invoked with three arguments: (value, index|key, collection). Iteratee functions may exit iteration early by explicitly returning false.

_.each gets only one Collection to iterate as the first argument, and a function "iteratee" for the second argument. So, for is a good option for you.

However, if you still want to use lodash you can get help from _range to create an array of indices, and then you can do something like that:

(in javascript of course)

let days = [5, 15, 25];
let hours = [6, 8, 7];
let result = [];
_.each(_.range(days.length),function(i) {
    if (days[i] < a && b < days[i+1]) {
        result.push(hours[i]);
    } else if (days[i] > a && days[i] < b) {
        result.push(hours[i]);
        if (i > 0) {
            result.push(hours[i-1]);
        }
    }
});
🌐
GitHub
github.com › lodash › lodash › issues › 804
_.findIndex returns -1 for an object key with value 0 · Issue #804 · lodash/lodash
When looking for a particular object key in an array, the index is returned as -1 if the value is 0. var objs = [{'a': 1}, {'b': 0}], index = _.findIndex(objs, 'b'); console.log(index); // -1 It is very much possible to have values as 0,...
🌐
DevDocs
devdocs.io › lodash~2
DevDocs — lodash 2 documentation
lodash 2.4.2 API documentation with instant search, offline support, keyboard shortcuts, mobile version, and more.
🌐
CodeToFun
codetofun.com › lodash › array-indexof
Lodash _.indexOf() Array Method | CodeToFun
November 21, 2024 - Optimize your JavaScript code with Lodash _.indexOf() Array Method. Effortlessly find the index of an element in your arrays, enhancing efficiency and performance. Explore the power of Lodash for seamless array operations on your web projects. Elevate your coding experience with this essential ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › indexOf
Array.prototype.indexOf() - JavaScript | MDN
const beasts = ["ant", "bison", "camel", "duck", "bison"]; console.log(beasts.indexOf("bison")); // Expected output: 1 // Start from index 2 console.log(beasts.indexOf("bison", 2)); // Expected output: 4 console.log(beasts.indexOf("giraffe")); // Expected output: -1 ... Element to locate in the array. ... Zero-based index at which to start searching, converted to an integer. Negative index counts back from the end of the array — if -array.length <= fromIndex < 0, fromIndex + array.length is used.