Spe*_*ark 8 arrays jquery serialization
我试图找出如何使用索引从serializedArray中删除项目.以下场景:
[
{ 'name' : 'item1', 'value' : '1' },
{ 'name' : 'item2', 'value' : '2' },
{ 'name' : 'item3', 'value' : 3 }
]
Run Code Online (Sandbox Code Playgroud)
现在我想删除'item2' - 我可以使用以下函数 - 但不知道如何删除它 - 是否有某种unset()方法或类似的东西:?
serializeRemove : function(thisArray, thisName) {
"use strict";
$.each(thisArray, function(index, item) {
if (item.name == thisName) {
// what to do here ?
}
});
}
Run Code Online (Sandbox Code Playgroud)
你可以使用普通 JS'filter()
方法,如下所示:
serializeRemove : function(thisArray, thisName) {
"use strict";
return thisArray.filter( function( item ) {
return item.name != thisName;
});
}
Run Code Online (Sandbox Code Playgroud)
filter()
使用回调函数来测试数组的每个元素。如果函数返回,则true
该元素将出现在结果中。如果返回false
,则该元素将被删除。
filter()
所有主流浏览器和IE9+均支持。