How to use indexOf in KnockoutJS?

An observableArray exposes a method called indexOf which is a wrapper to ko.utils. ArrayIndexOf that simply loops through the array looking for the item that you pass to it So, if you have the item you can do: var viewModel = { items: ko. ObservableArray({id: 1, name: "one"}, {id:2, name: "two"}) }; var item = viewModel.items" rel="nofollow">viewModel.items()1; console.

Log(viewModel.items" rel="nofollow">viewModel.items. IndexOf(item)); //equals 1 If you just have something like a key, then KO does have a utility function called ko.utils. ArrayFirst that just loops through the array trying to match the condition that you pass to it.

However, it returns the item and not the index of it. It would be slightly inefficient to get the object and then call indexOf on it, as you would make two passes through the array You could just write a loop yourself looking for the right item or write a generic function based on ko.utils. ArrayFirst that would look like: function arrayFirstIndexOf(array, predicate, predicateOwner) { for (var I = 0, j = array.

Length; I Log(arrayFirstIndexOf(viewModel.items" rel="nofollow">viewModel.items(), function(item) { return item. Id === id; })); //returns 1.

An observableArray exposes a method called indexOf, which is a wrapper to ko.utils. ArrayIndexOf that simply loops through the array looking for the item that you pass to it. So, if you have the item you can do: var viewModel = { items: ko.

ObservableArray({id: 1, name: "one"}, {id:2, name: "two"}) }; var item = viewModel.items" rel="nofollow">viewModel.items()1; console. Log(viewModel.items" rel="nofollow">viewModel.items. IndexOf(item)); //equals 1 If you just have something like a key, then KO does have a utility function called ko.utils.

ArrayFirst that just loops through the array trying to match the condition that you pass to it. However, it returns the item and not the index of it. It would be slightly inefficient to get the object and then call indexOf on it, as you would make two passes through the array.

You could just write a loop yourself looking for the right item or write a generic function based on ko.utils. ArrayFirst that would look like: function arrayFirstIndexOf(array, predicate, predicateOwner) { for (var I = 0, j = array. Length; I Call(predicateOwner, arrayi)) { return i; } } return -1; } Now, you can pass an array, a condition, and you will be returned the index of the first item that matches.

Var viewModel = { items: ko. ObservableArray({ id: 1, name: "one"}, { id: 2, name: "two"}) }; var id = 2; console. Log(arrayFirstIndexOf(viewModel.items" rel="nofollow">viewModel.items(), function(item) { return item.

Id === id; })); //returns 1.

Knockout. Js - How to use indexOf in KnockoutJS - Stack Overflow.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions