I have one array of strings. I need to find all strings starting with a key.
for eg: if there is an array [\'apple\',\'ape\',\'open\',\'soap\']
when searched with
With 2 improvements, can find if contains, not only as first char, and it has a third parameter to define the returned value : true means return just the number of element inside the array (index/key), false or anything not accepted or no value means return the complete string array[element], and if set to 'position=3', return the values of array which contains the string exactly in the ' after = number' position, here 3!
function find(key, array, returnindex)
{
var results = [];
if {returnindex.indexOf('position=') == 0}
{
var a = parseInt(returnindex.replace('position=',''));
returnindex = false;
}
else if (returnindex != true)
{
returnindex = false;
}
for (var i = 0; i < array.length; i++)
{
var j = '-'+array[i].indexOf(key);
if (typeof a == 'undefined')
{
if (i > 0)
{
j = 'ok';
}
}
else
{
if (i == a+1)
{
j = 'ok';
}
}
if (j == 'ok' && returnindex)
{
results.push(i);
}
else
{
results.push(array[i]);
}
}
return results;
}