这是一个代码示例:
var testFunction = function(x) {
delete(x); //x = null
}
var array = ["test1", "test2", "test3"];
$.each(array, function(e, e1) {
testFunction(e1);
}
我想在函数中删除数组中的元素。可以用索引(delete(array[1])
)删除它,但我不能在我的情况下这样做,因为删除功能应该稍后在on-Button-Press功能中(每个数组一个按钮 - 元素)(不像示例中那样)并且索引是动态变化的。
当前函数会删除/设置x变量null,它不会影响数组中的变量。
有没有办法删除变量后面的引用对象?也许用.getObject()
或类似的东西?
编辑: 完整的例子:
var createButton = function(object, placeThere) {
var oButton1 = new sap.ui.commons.Button({
text : ("Delete " + object.value),
});
//do some more not relevant stuff here
oButton1.attachPress(function() {
delete(object); /*deleteObjectOutOfArray*/
}
oButton1.placeAt(placeThere);
}
var array = [new Object(), new Object(), new Object()]; //array filled with objects with diffrent variables like for example value
$each (array, function(e1, e2) {
createButton(e2, "nameOfDiv");
}
答案 0 :(得分:0)
// Declare array first, so following code can refer to it
var array = [new Object(), new Object(), new Object()];
var createButton = function(object, placeThere) {
// then after not relevant stuff:
oButton1.attachPress(function() {
array.splice(array.indexOf(object), 1); // this removes object from array
}
// rest of code
}