Home > database >  How to remove/unset specific array in javascript?
How to remove/unset specific array in javascript?

Time:05-20

How to remove/unset specific array in javascript? I tried to use splice but i get different result. I think I'm missing something 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);

Current code output: [ 3, 150 ]

Expected output: [150]

CodePudding user response:

Filter is way to solve your problem:

var arr = [12, 3, 150];
var min = 100;
var max = 200


console.log(arr.filter(e => e > min && e < max))

  • Related