How can I remove a specific item from an array in JavaScript?

preview_player
Показать описание
#javascript
#arrays
#short
How do I remove a specific value from an array? Something like:

Constraints: I have to use core JavaScript. Frameworks are not allowed.
Рекомендации по теме
Комментарии
Автор

GPT explane:
This code creates an array of numbers [2, 5, 9] and logs it to the console using console.log().

The code then uses the indexOf() method to find the index of the number 5 in the array. The indexOf() method returns the index of the first occurrence of the specified value in the array, or -1 if it is not found.

Next, the code checks if the index of 5 is greater than -1. If it is, that means the item is found in the array, and the code uses the splice() method to remove the item at the specified index. The splice() method can be used to add or remove items from an array. In this case, the second parameter 1 specifies that only one item should be removed.

Finally, the code logs the updated array to the console using console.log(). The updated array [2, 9] no longer contains the number 5.

It's important to note that the splice() method modifies the original array, so the original array [2, 5, 9] has been changed to [2, 9].

codesamples
welcome to shbcf.ru