Marco V
Marco V

Reputation: 2653

Function to scan list of arrays for a character and return all arrays that contain it

I am trying to create a function that will scan all arrays for a character and return all arrays that contain that character. For example, how do i check if the first string in an array ends with "_"?

Example: searchNames([ [ "marco", "[email protected]" ], [ "john_", "[email protected]" ] ]);

//should output [ [ "john_", "[email protected]" ] ]

This is what I've done so far: http://jsfiddle.net/marcusdei/ve57p28h/

For those who prefer to see my code here:

function searchNames(logins){
 var str = "_";
 for (var i = 0; i != logins.length; i++) {
     var substring = logins[i];
     if (str.indexOf(substring) != - 1) {
         return substring;
        }
    }
        return null; 
}

Upvotes: 1

Views: 87

Answers (5)

Uri Goren
Uri Goren

Reputation: 13700

This looks like a classic use case for array's filter

var input=[ [ "marco", "[email protected]" ], [ "john_", "[email protected]" ] ];
var output=input.filter( function (x) {
//if want to see whether a '_' is found in the string
return x[0].indexOf('_')>-1;
//Or, only at the last position
//return x[0].length>0 ? x[0][x[0].length-1] == '_' : false;

} );

Upvotes: 1

Vikash
Vikash

Reputation: 1141

Applying JavaScript filter will do the job :

[ [ "marco", "[email protected]" ], [ "john_", "[email protected]" ] ].filter(
    function(data){ 
    return data[0]?data[0].lastIndexOf('_')==data[0].length-1:false;
});

Upvotes: 0

Travis J
Travis J

Reputation: 82297

jsFiddle Demo

I would suggest creating a special function to call for this scenario where you can filter based on a callback compare function.

function searchArray(arr2d,compare){
 return arr2d.filter(function(arr){
  return compare(arr);
 });
}

Once that is in place, you can take your input

var input = [ [ "marco", "[email protected]" ], [ "john_", "[email protected]" ] ];

And then call the search with the input and a callback

var result = searchArray(input,function(arr){ 
    var s = arr[0];//check that the first string
    return s[s.length-1] == "_";//ends in _
});//this callback function can be modified if the conditions change

stacksnippet

function searchArray(arr2d,compare){
 return arr2d.filter(function(arr){
    return compare(arr);
 });
}

var input = [ [ "marco", "[email protected]" ], [ "john_", "[email protected]" ] ];

var result = searchArray(input,function(arr){ 
    var s = arr[0];
    return s[s.length-1] == "_";
});

console.log(result);
document.getElementById('choice').innerHTML = JSON.stringify(result);
<div id="choice"></div>

Upvotes: 0

Amit.rk3
Amit.rk3

Reputation: 2417

Tweaked your code little bit. if you are looking for string ending with '_'

function searchNames(logins){
 var str = "_";
 var newArray=[];   
 for (var i = 0; i < logins.length; i++) {
     var substring = logins[i][0];
     if (substring[substring.length-1]==str) {
         newArray.push( logins[i]);
        }
    }
        return newArray; 
}

fiddle

Upvotes: 0

halbgut
halbgut

Reputation: 2386

There's a function called Array.prototype.filter. Here's a clean, functional approach.

function hasUnderscore (string) {
  return string.substr(-1) == '_' ? true : false
}

function doWithNthGetter (nth, func) {
  return function (array) {
    return func(array[nth])
  }
}

var checkThis = [ [ "marco", "[email protected]" ], [ "john_", "[email protected]" ] ]

checkThis.filter(nthGetter(2, hasUnderscore))

Upvotes: 0

Related Questions