How to use knockout to iterate over an object (not array)

前端 未结 8 543
不知归路
不知归路 2020-12-04 23:31

I want to use something similar to the Knockout foreach construct to iterate over the properties of an object. Here is what I am trying to create...

DESIRED

相关标签:
8条回答
  • 2020-12-05 00:36

    Simplified answer to work with any basic object, worked for me:

    <!-- ko foreach: {data: Object.keys(myObj)} -->
        <span data-bind="text: $data"></span> 
        <span data-bind="text: $parent.myObj[$data]"></span>
    <!-- /ko -->
    
    0 讨论(0)
  • 2020-12-05 00:37

    Supposedly, there is a deeper problem (see this thread at Google groups) that is that foreach treats the object as a dictionary of parameters, not as the collection to iterate.

    My best solution so far is to combined foreach in Object.keys(myobject) and 'with' binding context.

    0 讨论(0)
提交回复
热议问题