BaconJuice
BaconJuice

Reputation: 3779

Filtering thru an array and returning a new array with matching pattern

I'm trying to filter an array and return a new array with the matching elements.

So for example

var names = ["Kanye, West","Drake, Ross", "Rick, Boss", "Steven, Ross"];

function filterName(arr,pattern){
     //logic here
     return newArr
}

filterName(names,"ss"); //output ["Drake, Ross", "Rick, Boss", "Steven, Ross"];

Can someone point me in the right direction? Do I need regex to get something like this to work?

Thank you for reading!

Upvotes: 1

Views: 61

Answers (4)

Rahul Desai
Rahul Desai

Reputation: 15501

Looks like you want to create the RegEx dynamically.

You should be creating new RegExp() object and passing the pattern to it so we can use it with .test() method.

Working code snippet:

var names = ["Kanye, West","Drake, Ross", "Rick, Boss", "Steven, Ross"];

function filterName(arr, pattern){
  
  pattern = new RegExp(pattern);  // create a RegExp object with dynamically added pattern
  
  var newArr = [];  // initialize new array for storing results
  
  arr.forEach(function(item){  // parse the array
    if(pattern.test(item))  // if passes the regex test
      newArr.push(item);  // add to result array
  });

  return newArr;
}

// test our function 'filterName'

// test for 'ss'
console.log("Searching for 'ss'");
var result = filterName(names,"ss"); //output ["Drake, Ross", "Rick, Boss", "Steven, Ross"];
console.dir(result);

// test for 'West'
console.log("Searching for 'West'");
var result1 = filterName(names,"West"); //output ["Kanye, West"];
console.dir(result1);

Learn more:

Upvotes: 1

Brian Schermerhorn
Brian Schermerhorn

Reputation: 1431

You can use the Underscore.js library. Specifically the Filter function.

http://underscorejs.org/#filter

it would go something like this

_.filter(myArray, function(names){
    return names.indexOf("somePatternHere") !== -1;
});

For whichever values in the array return true will be included in the new array that is return with the "filtered" values.

There's also the Lo-dash library at https://lodash.com/

A reason I'd recommend using one of these libraries (probably Lodash actually) is they are optimized per the browser being utilized. Different browsers respond to native functions slightly differently and if you want the best results using one of these two is the best option.

Plus, it's a good habit to use these anyway since JavaScript lacks many basic functions that other libraries have already.

Upvotes: 0

Jeff
Jeff

Reputation: 12173

Use the Array.filter method - it's native (to modern browsers), requires no libraries.

var names = ["Kanye, West","Drake, Ross", "Rick, Boss", "Steven, Ross"];

function filterName(arr,pattern){
     return arr.filter(function(item) {
         return item.indexOf(pattern) > -1;
     });
}

var result = filterName(names,"ss"); //output ["Drake, Ross", "Rick, Boss", "Steven, Ross"];
document.write(JSON.stringify(result));

Upvotes: 1

dmgig
dmgig

Reputation: 4568

You can use jQuery grep:

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

Description: Finds the elements of an array which satisfy a filter function. The original array is not affected.

Upvotes: 0

Related Questions