0

I have been trying to create one remove function to remove items from different array. My function is like this, which accepts array name and index of the item in that array.

removeItem(itemArray, index){
   this.itemArray.splice(index,1);
}

Then use it like

removeItem('selectedColors',5);

But this does not catch my variable name. In console I get this.itemArray .splice is not a function I also tried like this, I created a itemArray variable so I could assign it. Well, did not work as well.

removeItem(itemArray, index){
   this.itemArray = itemArray;
   this.itemArray.splice(index,1);
}
juzraai
  • 4,835
  • 8
  • 26
  • 41
Jerry Li
  • 93
  • 7
  • 2
    Did you try it without `this`? – Jimenemex Jul 05 '18 at 20:10
  • 3
    so dot vs bracket notation? `this[itemArray].splice` – epascarello Jul 05 '18 at 20:10
  • 'this' references the context from which you call the function from, not the function itself. You don't need it when using variables that are passed into the function – Aram Becker Jul 05 '18 at 20:14
  • You may want to have a look at the 'this' keyword in javascript. It acts differently than other languages like Java/C#, and is kind of like an ever present reference to an object that serves as a context for your function. See here: https://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work – Dranyar Jul 05 '18 at 20:14
  • Seems weird everyone assumes it is an issue with `this` – epascarello Jul 05 '18 at 20:21
  • Your function accepts an array name and an index of an item to be removed? Why are you rewriting the splice method?`window[array_name].splice(index)` – lucas Jul 05 '18 at 21:20

4 Answers4

0

Just do

removeItem(itemArray, index){
    itemArray.splice(index,1);
}

And, if selectedColors is your array, don't use quotation marks.

removeItem(selectedColors, 5);
Marco
  • 38
  • 2
  • 8
0

In your example, you are messing things up or it's confusing on what you are trying to do. You cannot .splice a string since .splice only exists on arrays. You need to pass in an array to removeItem().

I also noticed that you need to remove the this keyword in front of itemArray. this is not the this you are trying to reference.

See below, and let me know if it helps.

function removeItem(itemArray, index) {
   itemArray.splice(index, index);
}
var itemArray = ['green', 'blue', 'red'];
console.log(itemArray);
removeItem(itemArray, 1);
console.log(itemArray);
Jimenemex
  • 2,836
  • 2
  • 13
  • 39
0

In JavaScript this references the context from which you call the function. What you could do is

removeItem.call(yourArray, index);

Then this would reference your array in your function. But in your case you can just remove the this because the passed in arguments are available without it.

Aram Becker
  • 729
  • 1
  • 8
  • 19
0

I can observe a general mistake here...

removeItem('selectedColors',5);

selectedColors is a string and not an array, string.splice() will always return an error. Make sure you pass the array to selectedColors.

let selectedColors = ["red", "blue"];
karthik
  • 1,040
  • 9
  • 21