indexof

Trouble using indexOf on a complex array [duplicate]

丶灬走出姿态 提交于 2019-11-26 21:19:01
问题 Possible Duplicate: indexOf method in an object array? I have a javascript array which follows this format: var arrayName = [ {id: "a", gender: "man", item: "stuff"}, {id: "b", gender: "woman", item: "stuff"}, {id: "c", gender: "man", item: "stuff"}, {id: "d", gender: "man", item: "stuff"} ]; Is there a way that I can use array.indexOf to find an index in the array, when for example I know the "id" variable. For example I tried; var position = arrayName.indexOf("b"); arrayName[position]

Cannot convert value of type 'Meme!' to expected argument type '@noescape (Meme) throws -> Bool'

此生再无相见时 提交于 2019-11-26 21:00:00
Here is the code: @IBAction func deleteMeme(sender: UIBarButtonItem) { if let foundIndex = MemeRepository.sharedInstance.memes.indexOf(selectedMeme) { //remove the item at the found index MemeRepository.sharedInstance.memes.removeAtIndex(foundIndex) navigationController?.popViewControllerAnimated(true) The error happens at the .indexOf method at (selectedMeme) . Cannot convert value of type Meme! to expected argument type @noescape (Meme) throws -> Bool Meme! is a struct for my app. How do I work through this? struct Meme { var topText : String! var bottomText: String! var image: UIImage! var

Get value of a string after a slash in JavaScript

左心房为你撑大大i 提交于 2019-11-26 19:00:15
问题 I am already trying for over an hour and cant figure out the right way to do it, although it is probably pretty easy: I have something like this : foo/bar/test.html I would like to use jQuery to extract everything after the last / . In the example above the output would be test.html . I guess it can be done using substr and indexOf() , but I cant find a working solution. 回答1: At least three ways: A regular expression: var result = /[^/]*$/.exec("foo/bar/test.html")[0]; ...which says "grab the

How to find the array index with a value?

我们两清 提交于 2019-11-26 18:12:32
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 . voigtan 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. Jaqen H'ghar For objects array use map with indexOf : var imageList = [ {value: 100}, {value: 200}, {value: 300}, {value: 400}, {value: 500} ]; var index = imageList.map(function (img) { return img.value; }).indexOf(200); console

How to get the index of an element in an IEnumerable?

时光总嘲笑我的痴心妄想 提交于 2019-11-26 17:48:51
I wrote this: public static class EnumerableExtensions { public static int IndexOf<T>(this IEnumerable<T> obj, T value) { return obj .Select((a, i) => (a.Equals(value)) ? i : -1) .Max(); } public static int IndexOf<T>(this IEnumerable<T> obj, T value , IEqualityComparer<T> comparer) { return obj .Select((a, i) => (comparer.Equals(a, value)) ? i : -1) .Max(); } } But I don't know if it already exists, does it? The whole point of getting things out as IEnumerable is so you can lazily iterate over the contents. As such, there isn't really a concept of an index. What you are doing really doesn't

Why is indexOf failing to find the object?

↘锁芯ラ 提交于 2019-11-26 16:01:10
问题 I created an integer list and am trying to return the index of a specific value. The array is 3,8,2,5,1,4,7,6 and I want to return the indexOf(3), which should be 0. I've tried the following in the Eclipse Java Scrapbook after importing java.util.*: int[] A = {3,8,2,5,1,4,7,9}; Arrays.asList(A).indexOf(3) I have also tried: int[] A = {3,8,2,5,1,4,7,6}; ArrayList<Integer> l = new ArrayList(Arrays.asList(A)); l.indexOf(3) Both are returning -1. Why? How to get this to work as expected? 回答1: It

Get the index of the nth occurrence of a string?

南笙酒味 提交于 2019-11-26 15:58:25
Unless I am missing an obvious built-in method, what is the quickest way to get the n th occurrence of a string within a string? I realize that I could loop the IndexOf method by updating its start index on each iteration of the loop. But doing it this way seems wasteful to me. That's basically what you need to do - or at least, it's the easiest solution. All you'd be "wasting" is the cost of n method invocations - you won't actually be checking any case twice, if you think about it. (IndexOf will return as soon as it finds the match, and you'll keep going from where it left off.) Alexander

c# Array.FindAllIndexOf which FindAll IndexOf

隐身守侯 提交于 2019-11-26 14:12:34
问题 I know c# has Array.FindAll and Array.IndexOf. Is there a Array.FindAllIndexOf which returns int[] ? 回答1: string[] myarr = new string[] {"s", "f", "s"}; int[] v = myarr.Select((b,i) => b == "s" ? i : -1).Where(i => i != -1).ToArray(); This will return 0, 2 If the value does not exist in the array then it will return a int[0]. make an extension method of it public static class EM { public static int[] FindAllIndexof<T>(this IEnumerable<T> values, T val) { return values.Select((b,i) => object

Java indexOf method for multiple matches in String

最后都变了- 提交于 2019-11-26 14:07:02
问题 I had a question about the indexOf method. I want to find multiple cases of "X" in a string. Suppose my string is "x is x is x is x", I want to find x in all of its index positions. But how do you do this for multiple cases? Is this even possible with indexOf? I did int temp = str.indexOf('x'); It find the first x. I tried to do a for loop where i is initialized to length of string and this did not work since I kept finding the first x over and over. for (int y = temp1; y >= 0;y-- ) { int

Why Array.indexOf doesn&#39;t find identical looking objects

独自空忆成欢 提交于 2019-11-26 13:22:40
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... Selvakumar Arumugam indexOf compares searchElement to elements of the Array using strict equality (the same method used by the ===, or triple-equals, operator). You cannot use === to