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!
More Posts related to JavaScript,
- How to Run JavaScript on Mac Terminal
- Get Current time in GMT/UTC using JavaScript
- How to yarn reinstall all Packages
- [javaScript] Convert text case to lowercase
- Get Device Screen Width and Height using javaScript
- Fix - npm start: sh: index.js: command not found
- npm WARN saveError ENOENT: no such file or directory, open /mnt/c/package.json
- JavaScript : Get url protocol HTTP, HTTPS, FILE or FTP
- JavaScript: Convert an Image into Base64 String
- JavaScript : Get current page address
- How to get query string in JavaScript HTML location.search
- Create React App using npm command with TypeScript
- JavaScript: Count Words in a String
- Add Animated Scrolling to Html Page Title Script
- How to send email from JavaScript HTML using mailto
- Javascript convert text case from uppercase to lowercase
- Submit html form on dropdown menu value selection or change using javascript
- Send Extra Data with Ajax Get or Post Request
- Fix: SyntaxError: The requested module does not provide an export named default
- Examples: Convert String to int in JavaScript
- 10 ways to Convert String to a Number in JavaScript
- Excel Fix: SECURITY RISK Microsoft has blocked macros from running because the source of this file is untrusted.
- Fix: ReferenceError: require is not defined in ES module scope [Node]
- [JavaScript] Remove all Newlines From String
- How to detect Browser and Operating System Name and Version using JavaScript
More Posts:
- Your Android SDK is missing, out of date or corrupted SDK Problem - Android-Studio
- Add Line Number before each line in Notepad++ using Column Editor - NotepadPlusPlus
- Fix - bash: man: command not found - Linux
- Install Visual Studio VS Code on Mac using Brew - MacOS
- How to check about details of Notepad++ text editor - NotepadPlusPlus
- MySQL Error :1007 SQLSTATE: HY000 (ER_DB_CREATE_EXISTS) Message: Can't create database '%s'; database exists - MySQL
- Unbound classpath container: JRE System Library [JavaSE-1.7] - Java
- Add comma or semicolon at end of each line Notepad++ - NotepadPlusPlus