javascript - How to remove item from arr2 but keep reference to arr2? - Stack Overflow

admin2025-04-17  2

what i want to get?

let arr2=['aa','bb','cc','dd'];
let arr3=arr2;
arr2.remove('bb');
console.log(arr2);//['aa','cc','dd'];
console.log(arr3);//['aa','cc','dd'];
arr3.remove('aa');//['cc','dd'];
console.log(arr2);//['cc','dd'];
console.log(arr3);//['cc','dd'];

here is my first thoughts about how to get: first: find index of item second switch position

let index=arr2.findIndex(item);
arr2[index]=arr2.at(-1);
arr2.pop();
//switch last one and ele of index

what i want to get?

let arr2=['aa','bb','cc','dd'];
let arr3=arr2;
arr2.remove('bb');
console.log(arr2);//['aa','cc','dd'];
console.log(arr3);//['aa','cc','dd'];
arr3.remove('aa');//['cc','dd'];
console.log(arr2);//['cc','dd'];
console.log(arr3);//['cc','dd'];

here is my first thoughts about how to get: first: find index of item second switch position

let index=arr2.findIndex(item);
arr2[index]=arr2.at(-1);
arr2.pop();
//switch last one and ele of index

Share Improve this question edited Feb 1 at 13:32 jabaa 6,9943 gold badges15 silver badges39 bronze badges asked Feb 1 at 11:18 yutingbaiyutingbai 11 silver badge1 bronze badge 5
  • Improve your question. – Vivekanand Vishvkarma Commented Feb 1 at 11:50
  • either you have a reference to the other array or not. not both. – Nina Scholz Commented Feb 1 at 11:59
  • 1 You have a reference to arr2. Are you asking how to copy the array so you don’t modify the original? – Dave Newton Commented Feb 1 at 12:47
  • If I correctly understand, you are asking for a remove method, that removes elements in-place. You can use .splice – jabaa Commented Feb 1 at 13:01
  • This question is similar to: How can I remove a specific item from an array in JavaScript?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – jabaa Commented Feb 1 at 13:32
Add a comment  | 

1 Answer 1

Reset to default 1

You can remove elements from an array in-place with .splice:

let arr2 = ['aa','bb','cc','dd'];
let arr3 = arr2;
remove(arr2, 'bb');
console.log(arr2);//['aa','cc','dd'];
console.log(arr3);//['aa','cc','dd'];
remove(arr3, 'aa');//['cc','dd'];
console.log(arr2);//['cc','dd'];
console.log(arr3);//['cc','dd'];

function remove(arr, item) {
  let index = arr.indexOf(item);
  if (index !== -1)
    arr.splice(index, 1);
}

You can also add this remove function to the array prototype, but I wouldn't do it. There are multiple good reasons to not do it. But if you really want the code in your question to produce the expected results, you can do this:

Array.prototype.remove = remove;

let arr2 = ['aa','bb','cc','dd'];
let arr3 = arr2;
arr2.remove('bb');
console.log(arr2);//['aa','cc','dd'];
console.log(arr3);//['aa','cc','dd'];
arr3.remove('aa');//['cc','dd'];
console.log(arr2);//['cc','dd'];
console.log(arr3);//['cc','dd'];

function remove(item) {
  let index = this.indexOf(item);
  if (index !== -1)
    this.splice(index, 1);
}

But again, it's technically possible, but it's bad practice and you shouldn't do it.

转载请注明原文地址:http://anycun.com/QandA/1744830539a88214.html