-1

I have an number array which contains floats, I've tried to use slice but with no luck as it does not work on float numbers (or I am doing something wrong here)

var array = [1.5, 1.7, 2.05, 2.2, 2.3, 2.4, 2.8, 3.3, 3.4, 3.59, 3.68, 3.9, 4, 4.1, 32, 33.6, 35, 39, 41.7, 42.88, 49, 53.09, 56, 59, 59, 69, 99, 129, 169, 169, 189, 229, 256.2]

Slice returns only a empty array

> array.slice(60, 250)
> []

Basically i want to get all numbers in range between 60, 250.

1
  • 1
    Use filter. arr.filter(num => num >= 60 && num <= 250). Commented Dec 18, 2017 at 11:34

1 Answer 1

1

Array#slice takes items from start index up to an end index, and returns a new array. Since your array doesn't have 60 items or more (indexes 60 to 250), it returns an empty array.

To remove items from an array using a condition on the value (between 60 and 250 for example), you can use Array#filter:

var array = [1.5, 1.7, 2.05, 2.2, 2.3, 2.4, 2.8, 3.3, 3.4, 3.59, 3.68, 3.9, 4, 4.1, 32, 33.6, 35, 39, 41.7, 42.88, 49, 53.09, 56, 59, 59, 69, 99, 129, 169, 169, 189, 229, 256.2];

var result = array.filter(function(n) {
  return n >= 60 && n <=250;
});

console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.