最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I for-each over a Knockout.JS observableArray? - Stack Overflow

programmeradmin2浏览0评论

All attempts to iterate over a KO observableArray have failed. The flow just jumps over the block like the array is empty.

It's not, because its bound to some HTML and the debugger shows 7 items.

I've tried a normal for with an indexer, an ECMA-5+ forEach and now KO's own arrayForEach utility.

var EditorViewModel = function (firstDayOfWeek) {

    this.firstDayOfWeek = firstDayOfWeek;
    this.days = ko.observableArray([]); // Added in server-side generated view below.

    // Reads the activity data from each day and constructs and uploads models.
    this.save = function () {

        var basket = [];

        // Construct the upload activity models.

        ko.utils.arrayForEach(this.days(), function(d) {

            ... // never falls into this block.

There's nothing much out on the web about this, so I guess its a no-brainer. I'm obviously messing it up somehow, but its eluding me this afternoon.

this.days array looks good to me.

Thanks, Luke

All attempts to iterate over a KO observableArray have failed. The flow just jumps over the block like the array is empty.

It's not, because its bound to some HTML and the debugger shows 7 items.

I've tried a normal for with an indexer, an ECMA-5+ forEach and now KO's own arrayForEach utility.

var EditorViewModel = function (firstDayOfWeek) {

    this.firstDayOfWeek = firstDayOfWeek;
    this.days = ko.observableArray([]); // Added in server-side generated view below.

    // Reads the activity data from each day and constructs and uploads models.
    this.save = function () {

        var basket = [];

        // Construct the upload activity models.

        ko.utils.arrayForEach(this.days(), function(d) {

            ... // never falls into this block.

There's nothing much out on the web about this, so I guess its a no-brainer. I'm obviously messing it up somehow, but its eluding me this afternoon.

this.days array looks good to me.

Thanks, Luke

Share Improve this question edited Mar 7, 2015 at 17:38 Luke Puplett asked Mar 7, 2015 at 17:29 Luke PuplettLuke Puplett 45.3k55 gold badges203 silver badges290 bronze badges 1
  • 1 Reproduce the problem. Set up a fiddle. Everything else is guesswork. – Tomalak Commented Mar 8, 2015 at 17:49
Add a ment  | 

2 Answers 2

Reset to default 6

here days is a observableArray which is nothing but a function , to iterate you need to read the value like days() , this is will give you a javascript array.

var days = ko.observableArray([1,3,4,5,6]);

days().forEach(function(v,i){
  alert(v);
});
<script src="https://cdnjs.cloudflare./ajax/libs/knockout/3.2.0/knockout-min.js"></script>

I don't know why its not working with the forEach methods, but I did get it going with a properly written traditional for index iterator.

for (dayIndex = 0; dayIndex < this.days().length; dayIndex++) { ... }
发布评论

评论列表(0)

  1. 暂无评论