如何在运行时向计算可观察量添加其他数组项

How to add an additional array item to a computed observable at runtime

本文关键字:添加 其他 数组 观察 运行时 计算      更新时间:2023-09-26

我试图将一个额外的值附加到一个可观察的淘汰赛中,但没有运气。希望有人能为我指出正确的方向。目前,我正在遍历一系列字段并构建一系列日期

//extract Primary dates from entities
report.PrimaryDateRangeAttributes = ko.computed(function () {
      return $.grep(entity.PrimaryAttributes(), function (item) { return item.DataType() === 'datetime' });
 });

一旦我构建了数组,我想添加一个额外的项目

report.PrimaryDateRangeAttributes.push('DateEntered');

但是,计算可观察量不支持推送。任何人都可以提供一些关于如何在对象初始化期间插入附加值的建议吗?

提前感谢,

由于每当需要值时调用计算函数来构建值(毕竟这是computed的目的),您只需在函数中添加它:

//extract Primary dates from entities
report.PrimaryDateRangeAttributes = ko.computed(function () {
      var rv = $.grep(entity.PrimaryAttributes(), function (item) { return item.DataType() === 'datetime' });
      rv.push('DateEntered');
      return rv;
});