0

I have an array in javascript like this (1,2,3,4,5,6,7,8) and i want to remove al the values that are smaller than 5. So the array that remains is (1,2,3,4). How to do this with javascript or jquery...

1
  • 1
    Your question conflicts with itself, please reword. Commented Mar 30, 2014 at 18:51

6 Answers 6

4

You can filter the array with array.filter()

var array = [1,2,3,4,5,6,7,8];

var new_array = array.filter(function(item) {
    return item < 5;
});

FIDDLE

or if you have to support IE8 and below, you can do it the old fashion way

var array = [1,2,3,4,5,6,7,8],
    new_array = [];

for (var i=0; i<array.length; i++) {
    if (array[i] < 5) new_array.push(array[i])
}

FIDDLE

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

1 Comment

xD jup. i forget about it. but yes, he's right. but be aware of browser compatiblity with nativ filter method. have a look here developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
1

I think you want to remove items larger than 5, but jquery grep should do it:

https://api.jquery.com/jQuery.grep/

1 Comment

if you don't want to load the big jquery libary, use underscore instead. in underscore the right method is named _filter. underscorejs.org/#filter
1

Use .map(). This will remove values less than 5 and remaining array values will be removed from array.

var arr = $.map( [ 1,2,3,4,5,6,7,8 ], function( n ) {
    return n < 5 ? n : null;
});
console.log(arr);

DEMO

or

Use .grep(). This will remove values less than 5 and remaining values will be removed from the array.

var arr = $.grep( [ 1,2,3,4,5,6,7,8 ], function( n ) {
    return n < 5;
});
console.log(arr);

DEMO

I will suggest you to go with grep based on jsperf result.

http://jsperf.com/map-vs-grep/2

4 Comments

you are not removing the wrong values you just change these values to null. but they are still there. better is $.grep() or .filter()
@ManuelRicharz: Could you please check array length it is showing 4. Hope map will work.
yes, the length is not counting null-values. but the memory is still allocated.
@ManuelRicharz: jQuery's map function is different than the native map function. It removes the array element if the callback returns null or undefined (see the documentation). And of course null values are considered for length.
0
var orig= [1,2,3,4,5,6,7,8];

just in case they're out of order:

var copy=orig.sort();

then:

var result = copy.splice(0,copy.lastIndexOf(4)+1);

http://jsfiddle.net/LXaqe/

Comments

0

In this case you can use JavaScript is good in compare of jQuery. Because JavaScript *Execution* fast compare to a jQuery.

Check this Demo jsFiddle

JavaScript

var filtered = [1,2,3,4,5,6,7,8].filter(issmall);

function issmall(element) {
    return element < 5 ;
}    

console.log(filtered);

Result

[1, 2, 3, 4] 

JavaScript filter() Method

Comments

0
Var arr = [1,2,3,4,5,6,7];
var newarr = [];
For(var i=0 ; i<=arr.length ; i++){
    if(arr[i] < 5){
       newarr.push(arr[i]);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.