Created
January 24, 2018 20:00
-
-
Save sod/b05f36fc2de48621686fbcdbaad634db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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