The best way to remove array element by value

后端 未结 10 1360
北荒
北荒 2020-12-25 10:18

I have an array like this

arr = [\"orange\",\"red\",\"black\",\"white\"]

I want to augment the array object defining a deleteElem()<

10条回答
  •  没有蜡笔的小新
    2020-12-25 10:41

    Here's how it's done:

    var arr = ["orange","red","black","white"];
    var index = arr.indexOf("red");
    if (index >= 0) {
      arr.splice( index, 1 );
    }
    

    This code will remove 1 occurency of "red" in your Array.

提交回复
热议问题