首页 > 解决方案 > 显示从通过 viewModel 循环的函数返回的列表

问题描述

我有一个函数,它接受一个容器并遍历其对象以执行搜索并检索找到的项目的数组。

ViewModel 有一个像这样传递的容器,例如:

    function ShowRoomBuildingsViewModel() {
            this.LoadModel = function (container) {
    
            for (var u in container.Payload.ShowRoom) {
                if (container.Payload.ShowRoom[u].Inventory!== undefined)
                    this.Content.push(container.Payload.ShowRoom[u].HVehicle, container.Payload.ShowRoom[u].Cars);
    }

    // Definitions
    this.Inventory= ko.observableDictionary();
    this.Content= ko.observableArray();

我正在尝试创建一个 ObservableArray 对象以在我的前端使用<!-- ko foreach -->. 下面的代码应该检索一个或多个结果。

this.Content = function (container) {
    var concatedList;               
    Content.forEach(element, elementIndex => {
        var Content1 = container.Payload.Inventory.HVehicle.find(el => el.ID == element.ID);
        if (Content1 != undefined) {
            concatedList[elementIndex] = element;
            concatedList[elementIndex].push(Content1);
        }                    
    });
    return concatedList;
};

我试过设置断点来查看里面发生了什么,但它没有进入。我不确定这是否是正确的方法,或者它是否有效,但它没有显示任何错误。在它填充的第一个循环中Content,我可以看到有五个结果。

任何建议将不胜感激。

标签: javascriptknockout.js

解决方案


推荐阅读