且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

替换 observableArray 中的项目

更新时间:2023-12-05 12:50:22

replace 函数接受两个参数,您要替换的项和要替换的新项.您正在传递索引代替要替换的项目,因此它不起作用.

The replace function accepts two parameters, the item you want to replace and the new item you want to replace it with. You are passing in the index in place of the item to replace so it doesn't work.

替换调用应该是:

self.locations.replace(oldLocation, new location(value));

顺便说一句,你不应该在那里需要 valueHasMutated() 调用,它会被 replace() 调用调用.

On a side note, you shouldn't need the valueHasMutated() call there, it will get invoked by the replace() call.

旁注,许多原生 Array 函数可用于可观察数组.它们被转发到底层数组值,根据需要触发突变通知.其中包括:

Side note, many of the native Array functions are available for observable arrays. They are forwarded to the underlying array value triggering notifications of mutations as needed. These include:

poppushreverseshiftsortspliceunshiftslice(只读).

pop, push, reverse, shift, sort, splice, unshift, slice (readonly).

Knockout 提供了这些额外的方法,应在此处(当前为 v3.5.1)记录:

Knockout provides these additional methods which should be documented here (currently v3.5.1):

removeremoveAlldestroydestroyAllindexOf替换排序反转