我的代码就像
var shapes1 = [ r.image("node.gif",190, 100, 47, 45)];
var shapes2 =[];
for (var i = 0, ii = shapes1.length; i < ii; i++) {
shapes1[i].mousedown(function(e){
var temp=this.clone();
shapes1.push(temp);
//now I want to remove "this" from shapes1
//and put it into shape2
//HOW??
isDrag=true;
e.preventDefault();
});
}
也许这是错误的做法?我应该使用一个类,但这不是 DOM 项吗?
我发现拥有类似的东西很方便
function removeIf(arr, predicate) {
for (var i = 0; i < arr.length; i++) {
if (predicate(arr[i])) {
arr.splice(i--, 1);
}
}
}
那么:
var shapes1 = [ r.image("node.gif",190, 100, 47, 45)];
var shapes2 =[];
for (var i = 0, ii = shapes1.length; i < ii; i++) {
shapes1[i].mousedown(function(e){
var temp=this.clone();
shapes1.push(temp);
removeIf(shapes1, function (item) { return item === this; });
shapes2.push(this);
isDrag=true;
e.preventDefault();
});
}
使用 splice(index, range) 从数组中删除项目。
const moveIndex = (arry, index, moveTo) => {
const saved = [];
for (let i = 0; i < arry.length; i++) {
saved[i] = arry[i];
if (moveTo > index) {
if (i >= index) {
arry[i] = arry[i + 1];
}
if (i >= moveTo) {
arry[moveTo] = saved[index];
if (i > moveTo) {
arry[i] = saved[i];
}
}
} else {
if (i >= moveTo) {
arry[moveTo] = saved[index];
if (i > moveTo) {
arry[i] = saved[i - 1];
}
}
if (arry[moveTo] == arry[i]) {
arry[i] = saved[index + 1];
}
}
}
return arry;
};