J.doe
J.doe

Reputation: 145

Delete numbers below a certain number in an array

Today i'm facing a really weird problem. I'm actually trying to delete numbers below a certain number in an array.

I have this array [1, 7, 2, 3, 90, 4, 70, 20] and I only want the numbers greater than 20 !

So I should have that in output : [90, 70]

But Instead I have this array : [7, 3, 90, 70, 20] ???

Here is my code :

function superior() {
  var arr = [1, 7, 2, 3, 90, 4, 70, 20]

  for (var i = 0; i < arr.length; i++) {
    if (arr[i] < 20) {
      arr.splice(arr.indexOf(arr[i]), 1);
    } else {
      break;
    }
  }

  return arr;
}

console.log(superior());

Upvotes: 1

Views: 1448

Answers (5)

Nicholas Tower
Nicholas Tower

Reputation: 85132

Mutating an array while you're looping through it is always going to be tricky. If you shrink the size of the array, but don't change i, then you're can end up interacting with the wrong element of the array the next time through the loop. Also, using break will stop the loop from running entirely, and so skip the rest of the array. You may have meant to use continue, but since you're on the last line of the loop anyway, continue isn't needed.

Rather than trying to change the array and loop through it at the same time, i'd recommend creating a new array. Then you can loop through the old one unhindered:

const arr = [1, 7, 2, 3, 90, 4, 70, 20]
const newArr = []

for (const i = 0; i < arr.length; i++) {
  if (arr[i] >= 20) {
    newArr.push(arr[i]);
  }
}

console.log(newArr)

Filtering an array like this is a very common thing to do, so there are built in tools to do it for you. Every array has a .filter method. You pass into it a function describing what elements you want to keep, and it will produce the new array for you:

const arr = [1, 7, 2, 3, 90, 4, 70, 20]
const newArr = arr.filter(element => element >= 20);

console.log(newArr)

Upvotes: 4

Shankar_programmer
Shankar_programmer

Reputation: 308

Use temporary array to push new values.

function superior() {
  var arr = [1, 7, 2, 3, 90, 4, 70, 20];
  temp_arr = [];

  for (var i = 0; i < arr.length; i++) {
    if (arr[i] > 20) {
      temp_arr.push(arr[i]);
    }    
  }
  return temp_arr;
}

console.log(superior());

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386736

You could iterate from the end and omit indexOf, because you have already the index i.

This appoach loops from the end and after splicing, the remaining lower indices remains.

function superior() {
  var array = [1, 7, 2, 3, 90, 4, 70, 20],
      i = array.length;

  while (i--) if (array[i] < 20) array.splice(i, 1);
  return array;
}

console.log(superior());

Upvotes: 1

Harun Yilmaz
Harun Yilmaz

Reputation: 8558

You can use Array.filter()

var arr = [1, 7, 2, 3, 90, 4, 70, 20] 

var filteredArr = arr.filter(item => item > 20);

console.log(filteredArr);

Upvotes: 1

Mihai T
Mihai T

Reputation: 17697

You can filter them out according to a condition.

And replace your existing array with the filtered one. Or if you don't want to replace it, use another variable and assign the filtered value to that variable.

var newArray = array.filter(item => item > 20)

Check .filter()

var array =  [1, 7, 2, 3, 90, 4, 70, 20];
array = array.filter(item => item > 20)
console.log(array)

Upvotes: 1

Related Questions