JavaScript arrays do not support deleting specified elements by default, such as

Let book_id = [1,2,3,4] book_id. Delete (4) // error, JS array does not have this methodCopy the code

Therefore, there are solutions

  • Using the Set Set

ES6 has a new Set type Set(), because collections support add(),delete(),remove(), forEach() and other operations, so you can directly convert Array() to Set().

Let book_id = new Set([1,2,3,4]) book_id. Delete (4) console.log(book_id) //Set {1,2,3}Copy the code

Note, however, that Set() itself is limited

A Set is an ordered list with no duplicate values, allowing fast access to the data it contains, adding a more efficient way to track discrete values

Therefore, ES5 native methods are used if the data you are working on does not meet the requirements of Set()

  • Use the indexOf ()

Instead, use indexOf() to find the index in the array and splice() to delete the element

Let book_name = ['1984',' Animal Farm ',' Rabble '] let I = book_name. IndexOf (' Animal Farm ') book_name. //['1984', 'mob']Copy the code