I'm working with a javascript project. I'm trying to use lodash tool to make life easier. I'd like to find a value in array if it exist. So, I select a findIndex() function. https://lodash.com/docs/4.17.15#findIndex
It's document says that the function will
Returns the index of the found element, else -1.
Then, I tried
console.log( _.findIndex([7,8,9], 7) );
The result is always -1
I think I did like the example already but I got only -1.
Please someone help.
The second parameter is [predicate=_.identity] (Function): The function invoked per iteration.
So you have to pass expression
instead of value
. Try like below.
console.log(_.findIndex([7,8,9], x => x === 7));
console.log(_.findIndex([7,8,9], x => x === 7));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js" integrity="sha256-VeNaFBVDhoX3H+gJ37DpT/nTuZTdjYro9yBruHjVmoQ=" crossorigin="anonymous"></script>
Why not use ES6 built in functions, instead of importing a library
const index = [7, 8, 9].findIndex(x => x === 7);
console.log(index);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.