[removed] move objects from one array to another: Best approach?

后端 未结 7 706
臣服心动
臣服心动 2021-01-04 01:38

I have two arrays, called \'objects\' and \'appliedObjects\'. I\'m trying to come up with an elegant way in Javascript and/or Angular to move objects from one array to anot

7条回答
  •  感动是毒
    2021-01-04 02:13

    If you wish to move simply whole array you could do:

    appliedObjects = objects;
    objects = []
    

    Of course it won't work if they were parameters of a function! Otherwise I cannot see other way than copying in the loop, e.g.

    while (objects.length) {
        appliedObjects.push(objects[0]);
        objects.splice(0,1);
    }
    

    or if you like short code :) :

    while (objects.length) appliedObjects.push(objects.splice(0,1));
    

    check fiddle http://jsfiddle.net/060ywajm/

提交回复
热议问题