Domanda

Il mio codice è come

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();
    });
}

Forse questo è il modo sbagliato per farlo? Dovrei usare una classe, invece, ma non è che per gli oggetti DOM?

È stato utile?

Soluzione

I trovarlo utile per avere cose come

function removeIf(arr, predicate) {

    for (var i = 0; i < arr.length; i++) {
        if (predicate(arr[i])) {
            arr.splice(i--, 1);
        }
    }
}

E allora:

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();
    });
}

Altri suggerimenti

Usa splice (indice, intervallo) rimuovere elementi da un array.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top