If you have an array object in JavaScript and want to remove items from it, you can make use of the indexOf() and splice() methods. method.
Explanation:- Say you have an array of fruits: var fruits = ['orange', 'mango', 'banana', 'grapes', 'plum'];
- You can get the index of any element/item as: var idx = fruits.indexOf('banana');
- Now you can use this index with slice() to remove the item banana from the array: fruits.splice(idx, 1);
Note: if the indexOf of the object is not found then you would get -1, so you can wrap the slice method with an if condition to check that.
Let's see an example JavaScript code snippet:
//Step 1: Note we have 5 elements/items in the array fruits
var fruits = ['orange', 'mango', 'banana', 'grapes', 'plum'];
//Let's print the array in console
console.log("Before removal: "+fruits);
//Step 2: Now lets get index of banana element that we want to remove
const idx = fruits.indexOf('banana');
//Step 3: Lets slice the element out
if(idx > -1)
fruits.splice(idx, 1);
//Step 4: Let's print the array again in the console to see the result.
console.log("After removal: "+fruits);
Output:
> "Before removal: orange,mango,banana,grapes,plum"
> "After removal: orange,mango,grapes,plum"
Comments:
- This was helpful. Thank you very much.
abidU 21 Aug 2020 12:08:49 GMT
- Further comments disabled!
- How to get UTC (GMT) time using JavaScript
- Submit html form on dropdown menu value selection or change using javascript
- How to detect Browser and Operating System Name and Version using JavaScript
- JavaScript : Get current page address
- [javaScript] Convert text case to lowercase
- How to check if a String contains substring or a word using javaScript
- Writing your first Hello, World! 🌍 JavaScript code Tutorial
- JavaScript: Check if variable is a number
- How to send email from JavaScript HTML using mailto
- Meaning of javascript:void(0) explained with example
- How to get query string in JavaScript HTML location.search
- Javascript convert text case from uppercase to lowercase
- Loading previous page using html button using JavaScript
- JavaScript : Get url protocol HTTP, HTTPS, FILE or FTP
- How to get current URL Location using Javascript HTML
- Detect if Cookies are enabled using JavaScript
- Write javaScript code in Swedish using FikaScript
- Add Animated Scrolling to Html Page Title Script
- JavaScript: Convert an Image into Base64 String
- Remove items from JavaScript array
- Send Extra Data with Ajax Get or Post Request
- How to Print from JavaScript HTML using window.print
- Get Device Screen Width and Height using javaScript
- Examples: Convert String to int in JavaScript
- npm WARN saveError ENOENT: no such file or directory, open /mnt/c/package.json
- INSTALL_FAILED_INSUFFICIENT_STORAGE Android Error - Android
- Setting up RSS feeds notifications within Outlook - HowTos
- Fix SharePoint 2019 installation error This product requires Visual C++ Redistributable Package for Visual Studio 2017 - SharePoint
- Fix zsh: permission denied: script.sh - zsh
- How to change the tab color on Microsoft Excel on Mac - MacOS
- Fix - sudo: systemctl: command not found - Ubuntu
- Permanently set $PATH variable in Mac Zsh shell - MacOS
- [Android Studio] How to locate code for activity_main.xml - Android-Studio