
Java
Javascriptconst _ = require('lodash');const numbers = [1, 2, 3, 4, 5];const index = _.findIndex(numbers, (num) => num === 3);console.log(index); // 输出:2在上面的例子中,我们有一个包含一些数字的数组。我们使用Javascriptconst _ = require('lodash');const numbers = [1, 2, 3, 4, 3, 5];const indexes = _.filter(_.map(numbers, (num, index) => num === 3 ? index : undefined), (index) => index !== undefined);console.log(indexes); // 输出:[2, 4]在上面的例子中,我们使用在 Lodash 中,可以使用 _.indexOf 和 _.findIndex 等方法来查找匹配元素的索引。如果你需要查找所有匹配元素的索引,可以使用 _.chAIn 和 _.filter 方法结合 _.map 和 _.indexOf 来实现。例如:
<code>Javascript<br/>_.chAIn(array) .filter(predicate) .map(item => _.indexOf(array, item)) .value()</code>这种方法会返回一个包含所有匹配元素索引的数组。
Copyright © 2025 IZhiDa.com All Rights Reserved.
知答 版权所有 粤ICP备2023042255号