Remove an Item from Array using UnderscoreJS Last Updated : 19 Jul, 2024 Comments Improve Suggest changes Like Article Like Report Underscore.js is a JavaScript library that provides a wide range of utility functions for working with arrays, objects, functions, and more. One common task when working with arrays is removing an item based on a specific condition. In this article, we will explore options to remove an item from an array using Underscore.js.Table of Content Using _.without() MethodUsing _.filter() MethodUsing _.reject() MethodApproach 1: Using _.without() MethodThe _.without() method creates a new array with all occurrences of a specified value removed. HTML <!DOCTYPE html> <html> <head> <script src= "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"> </script> </head> <body> <script> let array = [1, 2, 3, 4, 5]; let newArray = _.without(array, 3); console.log(newArray); </script> </body> </html> Output: This output will display on console.[1, 2, 4, 5]Approach 2: Using _.filter() MethodThe _.filter() method creates a new array with all elements that pass the test implemented by the provided function. HTML <!DOCTYPE html> <html> <head> <script src= "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"> </script> </head> <body> <script> let array = [1, 2, 3, 4, 5]; let newArray = _.filter(array, function(item) { return item !== 3; }); console.log(newArray); </script> </body> </html> Output:[1,2,4,5]Approach 3: Using _.reject() MethodThe _.reject() method is the opposite of _.filter(), creating a new array with all elements that do not pass the test implemented by the provided function. HTML <!DOCTYPE html> <html> <head> <script src= "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"> </script> </head> <body> <script> let array = [1, 2, 3, 4, 5]; let newArray = _.reject(array, function(item) { return item === 3; }); console.log(newArray); </script> </body> </html> Output:[1,2,4,5]Approach 4: Using _.findIndex() and Array.splice() MethodsAnother effective way to remove an item from an array using Underscore.js is by combining the _.findIndex() method with the native Array.splice() method. The _.findIndex() method helps find the index of the first element that satisfies a provided condition, and then Array.splice() can be used to remove the element at that index.Example: Implementation of a program to remove an item from an array using _.findIndex() and Array.splice() methods JavaScript const _ = require('underscore'); function removeItem(arr, condition) { const index = _.findIndex(arr, condition); if (index !== -1) { arr.splice(index, 1); } return arr; } // Example usage: let arr = [1, 2, 3, 4, 5]; console.log(removeItem(arr, num => num === 3)); arr = [10, 20, 30, 40, 50]; console.log(removeItem(arr, num => num === 20)); Output:[1,2,4,5][10,30,40,50] Comment More infoAdvertise with us Next Article Remove an Item from Array using UnderscoreJS V vkash8574 Follow Improve Article Tags : JavaScript Web Technologies JavaScript - Underscore.js JavaScript-Questions Similar Reads JavaScript Array Search And Remove String. Arrays are widely used to store and manipulate collections of data. Sometimes we may need to search for a specific string within an array and remove it. This operation can be useful in various cases, such as filtering out unwanted items, managing user inputs, or processing data from an external sour 2 min read How do I Remove an Array Item in TypeScript? In this article, we will learn about the different ways of removing an item from an array in TypeScript. In TypeScript, an array can be defined using union typing if it contains items of different types. We can use the following methods to remove items from a TypeScript array:Table of ContentUsing t 4 min read How to remove an item from an array in AngularJS Scope? In the AngularJS framework, we can manipulate the data that is within the scope, for example, we can perform the operations on an array by removing its elements. This helps us for better efficiency in developing the web application.We can remove the array elements in the applications like to-do list 5 min read Underscore.js Array Complete Reference Underscore.js is a lightweight JavaScript library and not a complete framework that was written by Jeremy Ashkenas that provides utility functions for a variety of use cases in our day-to-day common programming tasks. Array is a single variable that is used to store different elements. It is often u 2 min read How to remove specific value from array using jQuery ? Given an array elements and the task is to remove the specific value element from the array with the help of JQuery. There are two approaches that are discussed below: Approach 1: We can use the not() method which removes the element that we want. Later, use get() method to get the rest of the eleme 2 min read How to delete an item or object from the array using ng-click ? The task is to delete the item from the list when the button is clicked. This all should be done by using ng-click. This is done by using the splice() method. The syntax for the method is given below. Syntax for splice() function: array.splice(indexno, noofitems(n), item-1, item-2, ..., item-n) Exam 2 min read JavaScript - Delete an Item From an Array In Javascript, we do not have any array.remove() method for deleting the element. Here are the various methods we can delete an item from an array using JavaScript.Different Ways to Delete an Item From an Array Using JavaScript1. Using for loop and push() MethodThe push() method will not mutate the 4 min read How to Remove a Specific Item from an Array in JavaScript ? Given an Array, the task is remove specific item from an array in JavaScript. It means we have an array with N items, and remove a particular item from array.ExamplesInput: Arr = [ 10, 20, 30, 40, 50, 60 ] removeItem = 40 Output: [ 10, 20, 30, 50, 60 ]Table of ContentUsing splice() MethodUsing filte 3 min read How to remove falsy values from an array in JavaScript ? Falsy/Falsey Values: In JavaScript, there are 7 falsy values, which are given below falsezero(0,-0)empty string("", ' ' , ` `)BigIntZero(0n,0x0n)nullundefinedNaNIn JavaScript, the array accepts all types of falsy values. Let's see some approaches on how we can remove falsy values from an array in Ja 6 min read How to remove object from array of objects using JavaScript ? Removing an object from an array of objects in JavaScript refers to the process of eliminating a specific object from the array based on certain conditions, like matching a property value. This task is common in data manipulation, ensuring the array only contains the desired elements.There are two a 2 min read Like