indexof

How to find the array index with a value?

岁酱吖の 提交于 2019-11-26 03:48:53
问题 Say I\'ve got this imageList = [100,200,300,400,500]; Which gives me [0]100 [1]200 etc. Is there any way in JavaScript to return the index with the value? I.e. I want the index for 200 , I get returned 1 . 回答1: You can use indexOf: var imageList = [100,200,300,400,500]; var index = imageList.indexOf(200); // 1 You will get -1 if it cannot find a value in the array. 回答2: For objects array use map with indexOf : var imageList = [ {value: 100}, {value: 200}, {value: 300}, {value: 400}, {value:

Why Array.indexOf doesn't find identical looking objects

痴心易碎 提交于 2019-11-26 02:00:28
问题 I have array with objects. Something Like this: var arr = new Array( {x:1, y:2}, {x:3, y:4} ); When I try: arr.indexOf({x:1, y:2}); It returns -1 . If I have strings or numbers or other type of elements but object, then indexOf() works fine. Does anyone know why and what should I do to search object elements in array? Of course, I mean the ways except making string hash keys for objects and give it to array... 回答1: indexOf compares searchElement to elements of the Array using strict equality

Where is Java's Array indexOf?

大憨熊 提交于 2019-11-26 01:35:53
问题 I must be missing something very obvious, but I\'ve searched all over and can\'t find this method. 回答1: There are a couple of ways to accomplish this using the Arrays utility class. If the array is not sorted and is not an array of primitives: java.util.Arrays.asList(theArray).indexOf(o) If the array is primitives and not sorted, one should use a solution offered by one of the other answers such as Kerem Baydoğan's, Andrew McKinlay's or Mishax's. The above code will compile even if theArray

Why doesn't indexOf work on an array IE8?

这一生的挚爱 提交于 2019-11-26 01:20:51
问题 The below function works fine on Opera, Firefox and Chrome. However, in IE8 it fails on the if ( allowed.indexOf(ext[1]) == -1) part. Does anyone know why? Is there any obvious mistake? function CheckMe() { var allowed = new Array(\'docx\',\'xls\',\'xlsx\', \'mp3\', \'mp4\', \'3gp\', \'sis\', \'sisx\', \'mp3\', \'wav\', \'mid\', \'amr\', \'jpg\', \'gif\', \'png\', \'jpeg\', \'txt\', \'pdf\', \'doc\', \'rtf\', \'thm\', \'rar\', \'zip\', \'htm\', \'html\', \'css\', \'swf\', \'jar\', \'nth\', \

How to find indices of all occurrences of one string in another in JavaScript?

孤者浪人 提交于 2019-11-26 01:16:14
问题 I\'m trying to find the positions of all occurrences of a string in another string, case-insensitive. For example, given the string: I learned to play the Ukulele in Lebanon. and the search string le , I want to obtain the array: [2, 25, 27, 33] Both strings will be variables - i.e., I can\'t hard-code their values. I figured that this was an easy task for regular expressions, but after struggling for a while to find one that would work, I\'ve had no luck. I found this example of how to