Knockout.js 剔除将项添加到每个可观察数组中

Knockout.js 剔除将项添加到每个可观察数组中,knockout.js,computed-observable,knockout-3.0,Knockout.js,Computed Observable,Knockout 3.0,我有一个函数,代码如下: this.federalPriceComputed = ko.pureComputed( function() { return ko.utils.arrayMap(this.lineItems(), function(item) { var total = item.price() * item.federalTax() return formatCurrency(

我有一个函数,代码如下:

this.federalPriceComputed = ko.pureComputed(
        function() {
            return ko.utils.arrayMap(this.lineItems(), function(item) {
                var total = item.price() * item.federalTax()
                return formatCurrency(total);
            });
        }, this
    ).extend({ throttle: 500 });

this.statePriceComputed = ko.pureComputed(
            function() {
                return ko.utils.arrayMap(this.lineItems(), function(item) {
                    var total = item.price() * item.stateTax()
                    return formatCurrency(total);
                });
            }, this
        ).extend({ throttle: 500 });
当我运行代码时,它会显示未定义
statePriceComputed

我试图将所有代码都保存在模型对象中

如果我这样做,它会起作用

ko.utils.arrayForEach(myModel.lineItems(), function(item) {
   item.federalPriceComputed = ...
   item.statePriceComputed = .... 
   }
);
更多信息:

ko.observableArray.fn.withIndex = function (keyName) {  
    "use strict";
    var index = ko.computed(function () {
        var list, keys;
        list = this() || [];    // the internal array
        keys = {};              // a place for key/value
        ko.utils.arrayForEach(list, function (v) {
            if (keyName) {          // if there is a key
                keys[ko.utils.unwrapObservable(v[keyName])] = v;    // use it
            } else {
                keys[v] = v;
            }
        });
        return keys;
    }, this);

    // also add a handy add on function to find
    // by key ... uses a closure to access the 
    // index function created above
    this.findByKey = function (key) {
        return index()[key];
    };

    return this;
};
当我绑定变量时,我是这样做的

    if (stateTax.length) {
       stateTax.attr("data-bind", "text: formatCurrency($data.lineItems.findByKey(\"" + itemId + "\").statePriceComputed())");
    }

// similar code for federal tax

你能给出为什么选项1不起作用的建议吗

那你一定是做错了什么?下面的代码片段显示了#1个建议正在发挥作用

功能myApp(项目){
this.lineItems=ko.observableARay(items | |[]);
this.federalPriceComputed=ko.pureComputed(函数(){
返回ko.utils.arrayMap(this.lineItems(),函数(item){
var total=item.price()*item.federalTax();
返回总数;});
}扩展({throttle:500});
this.statePriceComputed=ko.pureComputed(函数(){
返回ko.utils.arrayMap(this.lineItems(),函数(item){
var total=item.price()*item.stateTax();
返回总数;});
}扩展({throttle:500});
}
var-app=新的myApp([
{price:ko.可观(5),stateTax:ko.可观(21),federalTax:ko.可观(2)},
{price:ko.可观(6),stateTax:ko.可观(12),federalTax:ko.可观(2)},
{price:ko.可观(10),stateTax:ko.可观(12),federalTax:ko.可观(3)},
{price:ko.可观(50),stateTax:ko.可观(21),federalTax:ko.可观(4)},
{price:ko.可观(8),stateTax:ko.可观(12),federalTax:ko.可观(2)},
{价格:可观测(75)、州税:可观测(21)、联邦税:可观测(3)});
ko.应用绑定(app)

联邦价格
国家价格

谢谢,但我没有使用foreach,而是使用jsf生成每个项目,我使用jquery添加了绑定。您提供了很多代码,但不是真正的重新编写。请将代码归结为实际问题,并在问题中显示复制它所需的所有(尽管尽可能少)代码,最好是在堆栈片段中。更一般的说明是,您应该查看,因为我觉得您可能有很多“
”问题。