Enumerating observableArray in knockout casting to string?

This question already has an answer here:

  • Why is using “for…in” with array iteration a bad idea? 25 answers

  • So, the issue is that for...in loops you over the keys to properties of an object, rather than values of the object.

    So if you have an object like

    var x = {
        a: "A",
        b: "B"
    }
    

    for...in will spit out 'a' and 'b' . Since arrays are objects, for...in with an array will give you something very similar: A string for each index into the array. So, with var a= [1, 2, 3] , for...in of a will result in '0', '1', '2' , rather than 1, 2, 3 .

    Use a construct like below to do what you want, instead.

    var sss = vm.tripData();
    for (var i = 0; i < sss.length; i++) {
        var sh2 = sss[i];
        sh2.isVisible(false);
    }
    
    链接地址: http://www.djcxy.com/p/70026.html

    上一篇: myArray [items] .toUpperCase不是函数

    下一篇: 枚举knockout中的observableArray将其转换为字符串?