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 object is not found then you would get -1, so you can wrap the slice method with a 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'];
//Lets 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 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!
More Posts related to JavaScript,
- Add Animated Scrolling to Html Page Title Script
- How to get query string in JavaScript HTML location.search
- How to get current URL Location using Javascript HTML
- How to detect Browser and Operating System Name and Version using JavaScript
- Submit html form on dropdown menu value selection or change using javascript
- How to Print from JavaScript HTML using window.print
- JavaScript : Get url protocol HTTP, HTTPS, FILE or FTP
- JavaScript : Get current page address
- How to send email from JavaScript HTML using mailto
- Send Extra Data with Ajax Get or Post Request
- Write javaScript code in Swedish using FikaScript
- Get Device Screen Width and Height using javaScript
- How to get UTC (GMT) using javascript
- Javascript convert text case from uppercase to lowercase
- Loading previous page using html button using JavaScript
- [javaScript] Convert text case to lowercase
- Detect if Cookies are enabled using JavaScript
More Posts:
- Fetch More then 10 Links Per Page in Google Search Result - Google
- Android Studio: Cannot perform refactoring operation - Android-Studio
- eclipse maven m2e : Cannot complete the install - Eclipse
- Top 3 Awesome Text Editors developers prefer at workplace - Sublime
- Calculate Area of a Rectangle - C-Program
- What is Bootstrap Jumbotron and how to use it - Bootstrap
- Background task activation is spurious error - Windows 10, Office 2016, Office 365 - HowTos
- How to remove old 404 pages ulrs from Google crawler - Html
- Exception in thread main java.lang.NoClassDefFoundError: package javaClass - Java
- SharePoint Server 2016 error Microsoft Office Online Server 2016 cant be installed on the same machine as a Microsoft SharePoint Server product - SharePoint
- 7 deadly java.lang.OutOfMemoryError in Java Programming - Java
- How to Print from JavaScript HTML using window.print - JavaScript
- Unable to establish connection to adb : Android Studio Error - Android
- How to send WhatsApp message from your Android App using Intent - WhatsApp
- Portable Notepad++ for windows - NotepadPlusPlus