javascript - indexOf and lastIndexOf is giving same thing in an otherwise different situation - Stack Overflow

var arrays = ['stew', 'rice', 'Beans', 'yam', 'plantain

var arrays = ['stew', 'rice', 'Beans', 'yam', 'plantain', 'potatoe',
  'margarine', 'barbeque', 'semovota', 'bournvita'
];
var arr3 = arrays.lastIndexOf('stew');
console.log(arr3);

var arrays = ['stew', 'rice', 'Beans', 'yam', 'plantain', 'potatoe',
  'margarine', 'barbeque', 'semovota', 'bournvita'
];
var arr3 = arrays.lastIndexOf('stew');
console.log(arr3);

Both indexOf and lastIdexOf gives thesame thing 0 in both cases how so when lastIndexOf counts from the back. the correct answer seems like 9

Share Improve this question edited Feb 7, 2018 at 11:54 F0XS 1,2693 gold badges16 silver badges19 bronze badges asked Feb 7, 2018 at 11:47 pedroyankypedroyanky 3232 silver badges11 bronze badges 2
  • Add stew somewhere in the list again, then you will see what it does – Pavlo Commented Feb 7, 2018 at 11:49
  • errr, because you only have one stew so it's in the same place. I think you need to re-read the manual about lastIndexOf: The index of the last occurrence of the specified value; -1 if not found. (not the index in reverse order) – Pete Commented Feb 7, 2018 at 11:54
Add a ment  | 

7 Answers 7

Reset to default 6

That is because either from starting or from backwards, "stew" is at same position which is 0.

From docs of lastIndexOf()

The lastIndexOf() method returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex.

You'll see the real difference when you have another "stew" in the middle or end.

var arrays = ['stew','rice','Beans','yam','plantain','potatoe',
      'margarine','barbeque','semovota','bournvita','stew'];
 var arr3 = arrays.lastIndexOf('stew');
 console.log(arr3); // 10

lastIndexOf() doesn't count from the back, it simply gives you the index of the last duplicate item searching for. If you put 'stew' again in the array somewhere past the 0 index it will give you its index while indexOf will give the first index.

example:

const array = ['stew','rice','Beans','yam','plantain', 'stew','potatoe','margarine','barbeque','semovota','bournvita'];

let regularIndex = array.indexOf('stew');
let lastIndex = array.lastIndexOf('stew');
console.log(regularIndex);
console.log(lastIndex);

// console prints:
// 0
// 5

lastIndex does not start counting from the back. It returns the index starting from the beginning of the last matching item.

"indexOf" -> what is the position of the first element.

"lastIndexOf -> what is the last element.

If you want the position of the last from back to front see below:

var listagem = ['stew', 'rice', 'semovota', 'stew', 'bournvita'];
var postFirstIndex = listagem.indexOf('stew');
var postLastIndex = listagem.lastIndexOf('stew');
var postLastIndexInverted = listagem.length - listagem.lastIndexOf('stew') - 1;
alert(postFirstIndex);
alert(postLastIndex);
alert(postLastIndexInverted);

There is only 1 value "stew" In your array, positioned at index 0. That is the first and last index for "stew", thus that is why both indexOf and lastIndexOf are returning you 0.

Add a second "stew" value to your arrays variable, then observe the output from your logs.

Also lastIndexOf does not start counting from the last index, it'd make no difference what direction that function searched from, as it would have to cycle through N elements, where N is the length of the given array.

Example below

var source = ["foo", "bar", "foo"];

console.log(source.indexOf("foo"));
//Above will log 0.

console.log(source.lastIndexOf("foo"));
//Above will log 2

console.log(source.lastIndexOf("bar"));
//Above will log 1

console.log(source.lastIndexOf("bar") === source.indexOf("bar"));
//Above will log true, as "bar" occurs only once in the array,
//just like "stew" in your question.

console.log(source.indexOf("cheese"));
//Above will log -1, just for fun ha ha

Hope that helps.

lastIndexOf gives you the the starting index but count last occurance inside the array from the right. In this there is single occurance so it is giving 0, but when you enter more than one occurance then you will see the difference:-

var arrays = ['stew','rice','Beans','yam','plantain','potatoe',
      'margarine','barbeque','semovota','stew','bournvita'];
       var arr3 = arrays.lastIndexOf('stew');
       console.log(arr3); // will give you 9
       var arr3 = arrays.lastIndexOf('margarine');
       console.log(arr3); // will give you 6 

now it give you 9. because stew es 2 time

lastIndexOf() doesn't count from the back, it simply gives you the index of the last duplicate item searching for. If you put 'stew' again in the array somewhere past the 0 index it will give you its index while indexOf will give the first index.

"indexOf" -> what is the position of the stew element at first occurance.

"lastIndexOf -> what is the position of the stew element at last occurrence.

if array would be like -> var arrays = ['stew', 'rice', 'Beans', 'yam', 'plantain', 'potatoe','margarine', 'barbeque', 'semovota', 'bournvita' , 'stew'];

arrays.lastIndexOf('stew') will return 10

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745104674a4611485.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信