最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove an item from a copy of an array without removing it from the original array - Stack Overflow

programmeradmin10浏览0评论

How can I remove an object from a copy of an array without removing it from the original?

I have a global variable :

    var userTrickList = [];

And inside a function, I make a copy of that global array :

    var tempUserTrickList = userTrickList;

Then I use the removeItem function that I created to remove a certain object.

    removeItem(considerTrick.IDName, tempUserTrickList);


    function removeItem(item, list) {
        //takes a string as 'item', finds in the array as 'list', 
        //then removes it from the list.
        for(var i = 0; i < list.length; i++)
        {
            if(item === list[i])
            {
                list.splice(i,1);
            }
        }
    }

My problem is, this function removes it from the userTrickList too.

Any ideas? It's definitely a problem in "removeItem(considerTrick.IDName, tempUserTrickList);", but I can't think of a solution.

How can I remove an object from a copy of an array without removing it from the original?

I have a global variable :

    var userTrickList = [];

And inside a function, I make a copy of that global array :

    var tempUserTrickList = userTrickList;

Then I use the removeItem function that I created to remove a certain object.

    removeItem(considerTrick.IDName, tempUserTrickList);


    function removeItem(item, list) {
        //takes a string as 'item', finds in the array as 'list', 
        //then removes it from the list.
        for(var i = 0; i < list.length; i++)
        {
            if(item === list[i])
            {
                list.splice(i,1);
            }
        }
    }

My problem is, this function removes it from the userTrickList too.

Any ideas? It's definitely a problem in "removeItem(considerTrick.IDName, tempUserTrickList);", but I can't think of a solution.

Share Improve this question edited May 15, 2014 at 6:03 bjb568 11.5k11 gold badges52 silver badges73 bronze badges asked May 15, 2014 at 5:48 Daniel ArcherDaniel Archer 671 silver badge5 bronze badges 0
Add a ment  | 

5 Answers 5

Reset to default 14

Use .slice(0) to clone an array.

var tempUserTrickList = userTrickList.slice(0);

Credits: http://davidwalsh.name/javascript-clone-array

use this function for your requirement

function removeElementFromArray(data,target) {
var temp=new Array();   
for ( var i = 0; i < data.length; i++) {
    if(data[i]!=target){
        temp.push(data[i]);
    }
}
return temp; }

here data is original array and target is the element you want to remove from array this function will return array without containing the removed item.

Try, It copy the original array

var tempUserTrickList = JSON.parse(JSON.stringify(userTrickList));

Demo CopyArray

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

Daniel's method of cloning an array is absolutely correct, but since I don't see an ES6-oriented answer, I'll offer up an alternative solution:

We can just as easily use the spread operator to clone an array, so we don't have to use the slice call.

const arr = [1, 2, 3];
const copy = [...arr];
copy.pop();
console.log(arr);  // returns [1, 2, 3]
console.log(copy); // returns [1, 2]
发布评论

评论列表(0)

  1. 暂无评论