mardi 10 décembre 2019

How to use array again after removing some elements on it

I have a sample array of:

let sampleArray = ["spider", "regards", "sorry"];

If I use if statement and remove some elements on it, how can I declare the new sample array with new value.

This is my code:

let sampleArray = ["banana", "apple", "orange", "grapes"];
     //let say the value of "fruits = apple"       
    if(sampleArray.includes(fruits)){ // will return TRUE, since 'apple' is included in the sampleArray

    //Now, I will remove it on the array
    sampleArray = sampleArray.filter(e => e !== fruits); 
    console.log("sampleArray: ", sampleArray) // return ["banana", "orange", "grapes"]

Now, how can I apply it in the SAME if statement again if(sampleArray.includes(fruits)) so that if I call it again the value is now ["banana", "orange", "grapes"] and my new fruits is grapes to have new array of ["banana", "orange"]

Is this possible? I can only use the statement (sampleArray.includes(fruits)) once.

Aucun commentaire:

Enregistrer un commentaire