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

后端 未结 7 639
臣服心动
臣服心动 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:23

    Now this maybe is not a fair answer, but if you notice you are doing alot of complicated object/array manipulations, you should really check out lodash or underscore library. then you could solve this with on liner:

    //lodash remove function
    appliedObjects.push.apply( appliedObjects, _.remove(objects, { 'selected': true}));
    
    //or if you want to insert in the beginning of the list:
    appliedObjects.splice(0, 0, _.remove(objects, { 'selected': true}));
    

提交回复
热议问题