Why doesn´t indexOf work?

in the code below; why is "self.ticketCollection.indexOf(t)" in the "self.Addticket-function always -1 ???

After page_load, I push New (newTicket), then ADD (addTicket). This works fine and a record of ("ny","-1") is added to my ticketCollection. Then when I repeat this another record exactly the same is added. Debugger (FireBug) tells me "self.ticketCollection.indexOf(t)" is -1 in both cases. Why?

function Ticket(ticketname, cost) {
    var self = this;

    //alert("ikke implementer fullt ut ennĂ¥!");

    self.ticketname = ko.observable(ticketname);
    self.cost= ko.observable(cost);

} // end Ticket


function ViewModel() {
var self = this;

//------ initiate ----------------------------------------------

// the product we want to view/edit
self.selectedTicket = ko.observable();

self.ticketCollection = ko.observableArray(
        new Ticket("Bus", "$2"),
        new Ticket("Ferry", "$3"),
        new Ticket("Bicycle", "$1")

// selected item from ticket list-view
self.listViewSelectedItem = ko.observable();

// push any changes in the list view to our main selectedTicket
self.listViewSelectedItem.subscribe(function (ticket) {
    if (ticket) {
}); // self.listViewSelectedItem.subscribe //

//---- NEW button pressed --------------------------------------
self.newTicket = function () {

        // create a new instance of a Ticket
        var t = new Ticket("ny", "-1");

        // set the selected Ticket to out new instance


//---- ADD to collection -----------------------------------------

self.addTicket = function () {
        //alert("ADD is pushed!");

        // get a reference to out currently selected product
        var t = self.selectedTicket();

        // ignore if null
        if (!t) { return; }

        // check to see that the ticket doesn\t already exist
        if (self.ticketCollection.indexOf(t) > -1) {

        // add the product to the collection

        // clear out the selected product

} // end ViewModel

Thanx in advance!

Asle :)


Just because you have two Ticket objects that contain the exact same values, does not mean these two objects are equal. This is why the indexOf returns -1 every time -- because the ticketCollection observable array does NOT contain the exact item you are checking for. This SO question has a good explanation of JavaScript object equality: How do you determine equality for two JavaScript objects?

Instead of using the indexOf on the array, you should instead check to see if any item in your ticketCollection matches BOTH the ticketName and the cost of the new, incoming Ticket.

I'll add an example of what I mean:

for(var i=0, len=self.ticketCollection().length; i < len; i++){
if ((t.ticketname() === ticketCollection()[i].ticketname()) && (t.cost() === ticketCollection()[i].cost())) {
    alert('Found the object in the observable array')

Need Your Help

Do we have Unmanaged resources in C#?

c# garbage-collection dispose unmanagedresources

I had a discussion with my friend about managed and unmanaged resources in c#.

ASP.NET session ID value


Relatively simple question regarding the value of session ID - Session ID is tied to a specific instance of the browser, correct (instance meaning window)? So I start with an empty desktop (WinXP or

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.