SSJS 从数组中删除项目

SSJS remove item from an array

在 ssjs 中,我有一个包含 jsonobjects 的数组。此数组存储在范围变量中并显示在重复控件中:

<xp:repeat id="rptAssessors" value="#{sessionScope.tmpAssessors}" var="obj" indexVar="idx">

我想从重复控件中添加选项以从数组中删除单个项目,例如通过每行按钮上的 onclick 事件:

<xp:eventHandler event="onclick" submit="true" refreshMode="partial" immediate="true" refreshId="dlgContent"> <xp:this.action> <xp:executeScript
 script="#{javascript:removeAssessor(idx);}"> </xp:executeScript> </xp:this.action></xp:eventHandler>   

我的 removeAssessor 函数如下所示:

function removeAssessor(idx){   

    var jsonObjArr = sessionScope.get("tmpAssessors");

    print("before elements " + jsonObjArr.length)
    print(idx + 1)
    jsonObjArr.splice(idx,1);
    print("after elements " + jsonObjArr.length)

    sessionScope.put("tmpAssessors",jsonObjArr);

}

我注意到 splice() 方法不起作用。我在一个 xsnippet 上读到这个方法在 SSJS 中不起作用,另一种方法作为 xsnippet 发布,但这是针对包含字符串

的数组

https://openntf.org/XSnippets.nsf/snippet.xsp?id=remove-an-entry-from-an-array-of-strings

谁能告诉我如何从数组中删除不是字符串的项目?

Array.splice 在 SSJS 中工作,但在 "inline way" 中你可能习惯于从 CSJS 中工作:在 CSJS 中,修改是内联完成的(没有创建新数组)并且删除的元素被 returned。在SSJS中,原始数组没有被修改,return值为数组的副本,不包括拼接的元素。

如果您将 jsonObjArr.splice(idx,1); 替换为 jsonObjArr=jsonObjArr.splice(idx,1);,您的代码应该可以正常工作。