Skip to content

Instantly share code, notes, and snippets.

@sod
Created January 24, 2018 20:00
Show Gist options
  • Save sod/b05f36fc2de48621686fbcdbaad634db to your computer and use it in GitHub Desktop.
Save sod/b05f36fc2de48621686fbcdbaad634db to your computer and use it in GitHub Desktop.
var convertToString = function(array) {
return array.map((item, index) => '"' + index + ' ' + item.replace(/"/g, '') + '"').join('');
}
var find = function(needle, sourceAsString, source) {
var rx = new RegExp('"(\\d+) ([^"]*' + needle + '[^"]*)"','gi');
var i = 0, results = [];
while (result = rx.exec(sourceAsString)) {
results.push(result[1]);
if (results.length >= 100) {
break;
}
}
return results.map((index) => ({
index: +index,
value: source[index]
}))
}
var array = ['foo', 'bar', 'baz'];
var haystack = convertToString(array);
console.log( array );
console.log( haystack );
console.log( find('f', haystack, array) );
console.log( find('b', haystack, array) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment