0

I'm using knockout js to create a list with a search function.

Result of the console.log:

enter image description here

As you can see the array supposedly has 5 objects in it but then the proto has 0. I don't understand what might be wrong.

var iniList = [
    new Spot("Park"),
    new Spot("High School"),
    new Spot("Soccer Stadium"),
    new Spot("Railway Station"),
    new Spot("Hospital")
];


var viewModel = {
    spots: ko.observableArray(iniList),
    filter: ko.observable(''),
    search: function(value) {
        console.log(iniList);

        viewModel.spots.removeAll();
        for (x = 0; x < iniList.length; x++) {
            console.log("iniList[x]");
            if (iniList[x].name.toLowerCase().indexOf(value.toLowerCase()) >= 0) {
                viewModel.spots.push(iniList[x]);
            }
        }
    }
};
Miguel Morujão
  • 1,131
  • 1
  • 14
  • 39
  • 2
    See also http://knockoutjs.com/documentation/observableArrays.html: “All of these functions are equivalent to running the native JavaScript array functions **on the underlying array**, and then notifying listeners about the change” – Ry- Jan 30 '17 at 03:15
  • @squint yes, because it never enters the for loop, I was trying to understand why – Miguel Morujão Jan 30 '17 at 03:19

1 Answers1

1

You're running "viewModel.spots.removeAll();" This removes all elements ofthe observable array.

llouk
  • 513
  • 4
  • 15