Afshin Mehrabani
Afshin Mehrabani

Reputation: 34939

How to filter an array?

Guys, please don't answer me to use a JavaScript library to solve this problem, I'm using VanillaJS.

Suppose I have an array with 10,000 string records, same as following:

var arr = [
  'John',
  'Foo',
  'Boo',
  ...
  'Some',
  'Beer'
];

Please note that the array doesn't follow any sort.

Now, I want to find items with oo in the text, what is the best way to do? Should I populate a new array or just pop items that don't match with the criteria?

Upvotes: 0

Views: 128

Answers (4)

user2810418
user2810418

Reputation:

A very simple way, use forEach:

var result = [];
arr.forEach(function (value) {
    if (value.indexOf('oo') !== -1) {
        result.push(value);
    }
});

or map:

var result = [];
arr.map(function (value) {
    if (value.indexOf('oo') !== -1) {
        result.push(value);
    }
});

Upvotes: 1

Naser
Naser

Reputation: 43

my approch

forEach function

function forEach(array, action) {
    for(var i=0; i<array.length; i++)
    action(array[i]);
}

partial function

function asArray(quasiArray, start) {
    var result = [];
    for(var i = (start || 0); i < quasiArray.length; i++)
        result.push(quasiArray[i]);
    return result;
}

function partial(func) {
    var fixedArgs = asArray(arguments, 1);
    return function() {
        return func.apply(null, fixedArgs.concat(asArray(arguments)));
    };
}

contains method for String obj

if (!String.prototype.contains) {
    String.prototype.contains = function (arg) {
        return !!~this.indexOf(arg);
    };
}

filter function:

function filter(test, array) {
    var result = [];
    forEach(array, function(element) {
        if (test(element))
            result.push(element);
    });
    return result;
}

test function for test array items

function test(key, el) {
    return el.contains(key);
}

finally

filter(partial(test, 'oo'), arr);

Upvotes: 2

bobthedeveloper
bobthedeveloper

Reputation: 3783

You can make use of the filter method, which will create a new array with all the elements that passes the condition.

arr.filter(function(x){ return x.indexOf ('oo') > -1});

If you want to use filter method in every browser you could add the polyfill method (see link) in your code.


Another option (slightly faster) with basic javascript would be:

Looping trough the array with a simple for loop and test on your condition.

var filtered = [];
for(var i=0, length=arr.length; i<length; i++){
   var current = arr[i]; 
   if(current.indexOf('oo') > -1){ 
      filtered.push(current);
   }
}

Upvotes: 3

Royi Namir
Royi Namir

Reputation: 148524

NO shortcuts ( p.s. you said I want to find items , not filter - hence my answer)

simple loop :

var g=arr.length; //important since you have big array
for( var i=0;i<g;i++)
{
 if ( g[i].indexOf('oo')>-1)  
  {
     console.log(g[i]);
  }
}

If you want to filter (without polyfill)

  var g=arr.length; //important since you have big array
  var h=[];
    for( var i=0;i<g;i++)
    {
     if ( g[i].indexOf('oo')>-1)  
      {
         h.push(g[i]);
      }
    }

    //do something with h

Upvotes: 1

Related Questions