What is the best way to clear a specific array in JavaScript?

I'm struggling with removing/unsetting a specific array in Javascript. I attempted to use the splice method, but it's not giving me the desired result. It seems like there's something that I'm overlooking here.

var arr = [12, 3, 150];
var min = 100;
var max = 200
for (var key2 in arr) {
    if (min > arr[key2] || arr[key2] >= max) {
        arr.splice(key2, 1);
    }
}
console.log(arr);

The current output of the code is: [ 3, 150 ]

However, my expected output is: [150]

Answer №1

Utilize Filter Function to tackle your issue:

let numbers = [12, 3, 150];
let minLimit = 100;
let maxLimit = 200;


console.log(numbers.filter(num => num > minLimit && num < maxLimit))

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble reloading an iframe src/location with a new URL in Safari?

Initially, my page loads with just a form inside an iframe. Here's an example: <iframe id="oIframe" ...src='somePage>' <form ... /> </iframe> Upon clicking a button in the form, certain javascript code is executed to cr ...

Exploring the comparison of information across two sets of databases using Node.js and MongoDB

In the realm of Node.js and MongoDB, I have set up two databases (1-btech2014 & 2-btechre2014) on a single server. My objective is to compare the data in btech2014 with that in btechre2014. If they match, I should retrieve the data from btech2014 as outpu ...

Transforming React js into typescript by incorporating data into constants and interfaces

Recently, I've delved into working on React Typescript and embarked on creating a dropdown component using Semantic UI. While Semantic UI offers code in JavaScript format that is easier to comprehend, I encountered a roadblock when attempting to conve ...

What is the best way to update information in a mongoose document using the _id field?

Although it may sound like a typical question, I've already conducted research and couldn't find a solution. I'm currently working on developing a discord bot and I don't have much experience in this area. The issue I keep encountering ...

Creating a large array of functions can be done by defining each function within the array individually, ensuring proper

I attempted to define an array of functions using the code below, but encountered issues with retrieving the value of 'i' outside of the loop. <script> var f = [] for (var i=0; i<1000; i++){ f[i] = function(){ return i } ...

It is not possible to make any further changes to the scene in THREE.js

Here is an example you can check out: Example Link I noticed that in newer versions of THREE.js, if the render function is called before adding additional objects to the scene, those objects will not be visible even with multiple subsequent calls to rend ...

Angular 4 showcases the information stored within this dataset

The data returned from an API to my Angular 4 application is not to my liking. Here is an example of the JSON, where I am only interested in the coin and its price: Goal is to display this data on the page: Coin Price BTC $4,281.28 ETH $294.62 ...

What could be causing the double invocation of render() in ReactNative?

I'm currently working on an app with a single screen displaying a map centered on the user's current coordinates. In my code below, I've set the latitude and longitude to null in the state of the App component. Using the componentDidMount() ...

Using AppCtrl's variable in another controller

Consider the following HTML code snippet: <html ng-app ng-controller="AppCtrl"> <head> <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script> <script src="script.js"></script> </head ...

Unleashing the power of multiple GET requests in node.js/express through

I have a route where I need to make multiple calls to the API server in order to gather all the necessary data. The code is working, but as I add a third call to the server, it's becoming hard to manage and read. I suspect that there's a better ...

Error message occurs when attempting to update a Mongoose document and encounters a TypeError related to the inability to read the property "coordinates"

Attempting my first document update REST api call, not entirely sure if I'm going about it the right way. I aim to have the api call only supply the fields of the document users want to update. My plan was to check for data in each field and add it i ...

Implementing a JQuery click method within a class structure

I'm having trouble getting the functionality of the .click function to work on my page unless I paste it into the browser console. In my class, this is what I have: var myClass = function(){ var toggleChecked = function(){ $('#myCheck ...

The THREE.js GLTFLoader is failing to load the 3D model

Hey there, I'm encountering an issue with displaying a 3D model using THREE.js GLTFLoader(). Below is the script I am working with: Here is the HTML file: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

By utilizing a switch case in PHP, a function is created with an array as parameters to effectively update the MySQL

Code Snippet: <form action="run.php" method="post"> Ambulance ID:<input type="text" name="amb_id"> Select one of the points below to submit: <input type="radio" name="tposition" value="1">t1 (1 km away from the signal) <input type="r ...

The functionality of the ReactJS button is lost once I start resizing the window

Welcome to the header.component.js file where the header component of my React app comes to life. I am currently working on adding a Font Awesome Icon with the "faBars" property: <Row onClick={onToggleMenu}> <Col> <FontAwes ...

Creating a dynamic textarea input based on the number entered using AngularJS

Can a loop be simplified in AngularJs to easily determine the number of textarea inputs a user can fill? <input type="number" class="form-control" min="1" max="4" value="1"> <div ng-repeat="..."> <div class="form-group"> < ...

Customize the footer of your KendoUI Grid with additional information retrieved from a

After receiving JSON data from my PHP script to populate a Kendo grid, I noticed an additional parameter called "querytime" which I want to display in the footer of the grid. I have attempted to access this parameter using JavaScript: console.log($(&apos ...

What is the best way to adjust the scroll speed within a specific element using React?

Hey there, I'm currently working on adjusting the scroll animation of a div (MUI Container) to make it smoother and slower. I've spent some time searching online for a solution but haven't found anything helpful so far... By the way, I' ...

Display solely the dates on the timeline chart of Google charts

I am facing an issue with my timeline chart script that I am using. The code snippet is as follows: google.charts.load("current", {packages:["timeline"],'language': 'pt'}); google.charts.setOnLoadCallba ...

Discover the array of results by implementing a while loop in JavaScript

My goal is to create a list of outputs that are not evenly divisible by numbers smaller than the input value. For example, if the input value is 10, the list should be 10, 9, 8, 7, 6, 4, 3, 1. I have written some code in JavaScript for this purpose, but ...