3

My code is like

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

Maybe this is the wrong way to do it? I should be using a class instead, but isn't that for DOM items?

1
  • Maybe you should describe what exactly you are trying to do. Commented Mar 22, 2010 at 13:06

2 Answers 2

3

I find it handy to have things like

function removeIf(arr, predicate) {

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

So then:

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();
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

No problem. The nice thing about passing in a function to test each item is that you can use it to delete multiple items based on any "query" you want, e.g. instead of item === this it could be item.firstName.length > item.lastName.length to delete any item with firstName longer than lastName (okay, that's just a silly example, but you see how powerful it is).
1

Use splice(index, range) to remove items from an array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.