How to use the lastIndexOf function from lodash

Find comprehensive JavaScript lodash.lastIndexOf code examples handpicked from public code repositorys.

lodash.lastIndexOf is a method from the Lodash library that finds the last index of a given element in an array or string.

5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
* @param {*} value The value to search for.
* @param {number} [fromIndex=array.length-1] The index to search from.
* @returns {number} Returns the index of the matched value or `-1`.
* @example
*
* _.lastIndexOf([1, 2, 3, 1, 2, 3], 2);
* // => 4
*
* _.lastIndexOf([1, 2, 3, 1, 2, 3], 2, 3);
* // => 1
fork icon73
star icon711
watch icon29

+ 3 other calls in file

238
239
240
241
242
243
244
245
246
247
module.exports.keyBy               = _.keyBy;
module.exports.keys                = _.keys;
module.exports.keysIn              = _.keysIn;
module.exports.kv                  = _.kv;
module.exports.last                = _.last;
module.exports.lastIndexOf         = _.lastIndexOf;
module.exports.lowerCase           = _.lowerCase;
module.exports.lowerFirst          = _.lowerFirst;
module.exports.lt                  = _.lt;
module.exports.ltContrib           = _.ltContrib;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

How does lodash.lastIndexOf work?

lodash.lastIndexOf is a method that returns the index of the last occurrence of a specified value in an array or string, searching from right to left. It accepts an optional starting index to begin the search.

475
476
477
478
479
480
481
482
483
484
params: [num1, num10, num100, num1000],
benchmarks: (array) => {
  const first = 1
  return {
    iiris: () => A.lastIndexOf(first, array),
    lodash: () => _.lastIndexOf(array, first),
    ramda: () => R.lastIndexOf(first, array),
    native: () => array.lastIndexOf(first),
  }
},
fork icon1
star icon31
watch icon0

81
82
83
84
85
86
87
88
89
90
91
92
93
console.log(join); // => 'a~b~c'


const last = _.last([1, 2, 3]);
console.log(last); // => 3


const lastIndexOf = _.lastIndexOf([1, 2, 1, 2], 2);
console.log(lastIndexOf); // => 3


const nth = _.nth(['a', 'b', 'c', 'd'], 1);
console.log(nth); // => 'b'
fork icon0
star icon4
watch icon0

+ 15 other calls in file

Ai Example

1
2
3
4
5
6
7
8
const _ = require("lodash");

const array = [2, 1, 3, 1, 2];
const value = 1;
const fromIndex = 3;

const index = _.lastIndexOf(array, value, fromIndex);
console.log(index); // Output: 3

In this example, we have an array of numbers array and we want to find the last index of the value 1 in the array starting from the index 3. We pass the array, the value to search for and the optional fromIndex parameter to _.lastIndexOf and it returns the index 3.

246
247
248
249
250
251
252
253
254
255
256
let last1 = _.last([1, 2, 3]);
console.log('last1--->', last1);
//last1---> 3


//_.lastIndexOf(array,value,[fromIndex=array.length-1])
let lastIndexOf1 = _.lastIndexOf([1, 2, 1, 2], 2);
console.log('lastIndexOf1--->', lastIndexOf1);
//lastIndexOf1---> 3
let lastIndexOf2 = _.lastIndexOf([1, 2, 1, 2], 2, 2);
console.log('lastIndexOf2--->', lastIndexOf2);
fork icon0
star icon0
watch icon0

593
594
595
596
597
598
599
600
601
602
ledLightArray.forEach((ledObject) => {
	newPosition = Math.floor(ledObject.ledIndex/2);
	processedPositions.push(newPosition);
	// If there are two instances of this position, then two trains are passing
	// each other and the LED should be yellow
	if (_.indexOf(processedPositions, newPosition) != _.lastIndexOf(processedPositions, newPosition)) {
		newColor = "yellow";
		// If they are passing in a station, paint it white
		if (newPosition == upnwOgilve ||
		    newPosition == palatine ||
fork icon0
star icon0
watch icon0

Other functions in lodash

Sorted by popularity

function icon

lodash.get is the most popular function in lodash (7670 examples)