10 examples of 'javascript find string in array' in JavaScript

Every line of 'javascript find string in array' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
16function findString(stringToSearch, uint8Array) {
17 const result = [];
18 const searchString = stringToSearch
19 .split('')
20 .map((char) => char.charCodeAt(0));
21
22 const maxIndex = uint8Array.length - stringToSearch.length;
23 let index = -1;
24 for (const char of uint8Array) {
25 index++;
26 if (index > maxIndex) {
27 return result;
28 }
29 if (char === searchString[0]) {
30 if (_findArray(searchString, index, uint8Array)) {
31 result.push(index);
32 if (result.length > MAX_ELEMENTS_TO_SEARCH) {
33 return result;
34 }
35 }
36 }
37 }
38 return result;
39}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

4function checkArrayForString(arr, string) {
5 for(let i = 0; i < arr.length; i++) {
6 if(arr[i].indexOf(string) > -1) {
7 return true;
8 }
9 };
10 return false;
11}
7export function findObject (array, property, value) {
8 for (let i = 0; i < array.length; i++) {
9 if (array[i][property] === value) {
10 return array[i]
11 }
12 }
13
14 return null
15}
112function findAll(str: string, toFind: string): number[] {
113 var matches = [];
114 var i = str.indexOf(toFind);
115 while (i !== -1) {
116 matches.push(i);
117 i = str.indexOf(toFind, i + toFind.length);
118 }
119 return matches;
120};
13function find (array, id) {
14 if(!id) return
15 for (var k in array) {
16 if(array[k].id == id) return array[k]
17 }
18}
30function indexOf (v, s) {
31 for (var i = 0; i < 7; i++) {
32 if (s[i][0] === v) return i
33 }
34 return -1
35}
62function replaceString(pString,pSearch,pReplace)
63// replaces in the string "pString" multiple substrings "pSearch" by "pReplace"
64{
65 //alert("cstring.js - replaceString() "+pString);
66 if (!pString) {
67 alert("replaceString()-Call - pString not defined!");
68 } else if (pString != '') {
69 {
70 //alert("cstring.js - replaceString() "+pString);
71 var vHelpString = '';
72 var vN = pString.indexOf(pSearch);
73 var vReturnString = '';
74 while (vN >= 0)
75 {
76 if (vN > 0)
77 vReturnString += pString.substring(0, vN);
78 vReturnString += pReplace;
79 if (vN + pSearch.length < pString.length) {
80 pString = pString.substring(vN+pSearch.length, pString.length);
81 } else {
82 pString = ''
83 }
84 vN = pString.indexOf(pSearch);
85 };
86 };
87 return vReturnString + pString;
88 }
89
90};
4function stringContains(string, value) {
5 return string.indexOf(value) !== -1
6}
1export function searchAndReplace(str, find, replace) {
2 return str.split(find).join(replace)
3}
9function in_array(needle, array){
10 for (var index = 0, length = array.length; index < length; index++) {
11 if (needle.toString() === array[index]) {
12 return true;
13 }
14 }
15 return false;
16}

Related snippets