Skip to content

Instantly share code, notes, and snippets.

@Kaylebor
Last active March 31, 2017 15:27
Show Gist options
  • Save Kaylebor/f0d8dfd03f1bcee2e7a56ea569bc28bc to your computer and use it in GitHub Desktop.
Save Kaylebor/f0d8dfd03f1bcee2e7a56ea569bc28bc to your computer and use it in GitHub Desktop.
Processing of arrays of strings
/* Checks if a string is contained inside the given array of strings
* If the second variable is a string, it just compares them
* If the second variable is neither string or array, it returns false */
function checkIfStringInArray(str, arr) {
if (typeof arr === 'string')
return str === arr;
if (arr.constructor && arr.constructor === Array)
for (var i in arr) {
if (str === arr[i]) {
return true;
}
}
return false;
}
//Accepts an array of arrays of strings, and returns a single array of unique strings
function concatMultiDimensionalStrArr(strArr) {
var finArr = []
for (var i in strArr) {
for (var j in strArr) {
if (!checkIfStringInArray(strArr[i][j], finArr) && strArr[i][j]) {
finArr.push(strArr[i][j])
}
}
}
return finArr
}
//Accepts an array of strings, and returns a single array of unique strings
function concatStrArr(strArr) {
var finArr = []
for (var i in strArr) {
if (!checkIfStringInArray(strArr[i], finArr) && strArr[i]) {
finArr.push(strArr[i])
}
}
return finArr
}
function escapeQuotes(array) {
var int_arr = [];
for (var i in array) {
int_arr.push(array[i].trim().replace('\'','\\\''));
}
return int_arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment