The syntax for the lastIndexOf is:
string.lastIndexOf(searchValue[, fromIndex])
where fromIndex is optional and represents the index from which to start looking. However, the search is performed backwards, so starting from position 3 in your example would be searching through string "This";
Videos
The syntax for the lastIndexOf is:
string.lastIndexOf(searchValue[, fromIndex])
where fromIndex is optional and represents the index from which to start looking. However, the search is performed backwards, so starting from position 3 in your example would be searching through string "This";
The search is done backwards. So if the "start" is 3, it will start at the forth character and work backwards. That means it will only search from the s in "This" and work backwards, which does not contain "apple".
You can map into an array of booleans:
var lastIndex =sample.map(s =>
s.id === 0).lastIndexOf(true);
then access your array by last index:
console.log(sample[lastIndex]);
Array's lastIndexOf method compares searchElement to elements of the Array using strict equality (the same method used by the ===, or triple-equals, operator). If your array contains objects, then you have to use another method.
If performance is not important and the amount of data is not that big, you can use
const lastIndex = sample.length - 1 - sample
.slice()
.reverse()
.findIndex( item => item.id === 0 );
slice will create a copy of the array, reverse will reverse it, findIndex will return the first item that matches o.id === 0 and the final result is subtracted from sample.length - 1. It's not very efficient for a large data set.
Or you can use a plain for
function findLastIndexOf(arr) {
for (let i = arr.length; i--;) {
if (arr[i].id === 0) {
return i;
}
}
}
findLastIndexOf(sample);
for (let i = arr.length; i--;) looks weird but it will start iterating from the last position and stop when i reach the value of 0. Give it a try.
Hope it helps
There are many ways to achieve it.
All depends on Your "creativity".
I'll write 3 of them:
1) Straight looping until last match:
const lastIndexOf = (haystack, needle) => {
let index = -1;
haystack.forEach(function(element, i) {
if (element === needle) index = i;
});
return index;
}
let fruits = ['apple', 'mango', 'pear', 'strawberry', 'bananas', 'mango', 'cherry']
console.log('Index of:', fruits.indexOf('mango'));
console.log('Last Index of:', lastIndexOf(fruits, 'mango'));
console.log('Last Index of:', lastIndexOf(fruits, 'potato'));
console.log(lastIndexOf([ 0, 1, 4, 1, 2 ], 1), "=?", 3);
2) Looping using -1 step and stopping at first match:
const lastIndexOf = (haystack, needle) => {
for (let i = haystack.length -1; i >= 0; i--) {
if (haystack[i] === needle) return i;
}
return -1;
}
let fruits = ['apple', 'mango', 'pear', 'strawberry', 'bananas', 'mango', 'cherry']
console.log('Index of:', fruits.indexOf('mango'));
console.log('Last Index of:', lastIndexOf(fruits, 'mango'));
console.log('Last Index of:', lastIndexOf(fruits, 'potato'));
console.log(lastIndexOf([ 0, 1, 4, 1, 2 ], 1), "=?", 3);
3) Reverse sorting + "length math":
const lastIndexOf = (haystack, needle) => {
const rIndex = haystack.reverse().indexOf(needle);
return (rIndex > -1) ? haystack.length - rIndex - 1 : -1;
}
let fruits = ['apple', 'mango', 'pear', 'strawberry', 'bananas', 'mango', 'cherry']
console.log('Index of:', fruits.indexOf('mango'));
console.log('Last Index of:', lastIndexOf(fruits, 'mango'));
console.log('Last Index of:', lastIndexOf(fruits, 'potato'));
console.log(lastIndexOf([ 0, 1, 4, 1, 2 ], 1), "=?", 3);
P.S. In case of very big arrays these 3 methods can be less optimal, since You cannot predict the value You're looking for is near to end or beginning of array.
So for such cases You can inspire from binary tree algorithm.
Everything depends on complexity of task.
Just go from the last element and return if you find what you are looking for.
Last index would be array.length - 1. Use classic for loop.
Good luck in your study!
The indexOf() method is case-sensitive, that's why you got these results, check the documentation. You can use the .toLowerCase() or the .toUpperCase() function before check with indexOf(). You need to use any of them on both of the strings you test.
For example with .toLowerCase(), change your code to:
test={product.name.toLowerCase().indexOf(filterText.toLowerCase())}
And
if(product.name.toLowerCase().indexOf(filterText.toLowerCase())===false)
You can Try to use includes method in lodash library. This should work.
_.includes(product.name.toLowerCase(), filter.toLowerCase());