-2

I made a code to detect if an array includes something and delte it...

var arr = ["hi", "bye"," oh no"] 

if (arr.includes("bye")) {
    //remove it from arr
} 

note that the place of bye can change so just imagine you don't know its place... What can I do?

  • And the problem is? -> [How do I ask and answer homework questions?](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) – Andreas Dec 29 '20 at 10:47
  • 3
    Does this answer your question? [How can I remove a specific item from an array?](https://stackoverflow.com/questions/5767325/how-can-i-remove-a-specific-item-from-an-array) – Kapobajza Dec 29 '20 at 10:48

2 Answers2

0

i think you need this

var arr = ["hi", "bye"," oh no"] 

var doRemove = "bye";
if (arr.includes(doRemove)) {
    arr.splice(arr.indexOf(doRemove), 1);
} 
console.log('arr', arr);
Mohammad Abedi
  • 495
  • 2
  • 7
0

You can use indexOf function of javascript to check if a value exists or not and than remove it

var arr = ["hi", "bye","oh no"] 

var index = arr.indexOf("bye")
if (index !== -1) {
    arr.splice(index, 1);
} 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf

Mamta
  • 6,426
  • 1
  • 13
  • 31