10 examples of 'js check if string in array' in JavaScript

Every line of 'js check if 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
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}
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

75function strInArray(str, ar) {
76 if (typeof ar === 'undefined') {
77 ar = exports.out;
78 }
79
80 for (var i in ar) {
81 if (ar[i].indexOf(str) !== -1) {
82 return true;
83 }
84 }
85
86 return false;
87}
119function checkIsNumberArrayString(str) {
120 if (!str.match(/^\s*(((\s*(-|\+)?((\.\d+)|(\d+\.\d+)|(\d+)|(\d+(\.\d+)?e(-|\+)?(\d+)))\s*,){0,2}(\s*(-|\+)?((\.\d+)|(\d+\.\d+)|(\d+)|(\d+(\.\d+)?e(-|\+)?(\d+)))))|((\s*(-|\+)?((\.\d+)|(\d+\.\d+)|(\d+)|(\d+(\.\d+)?e(-|\+)?(\d+)))\s){0,2}(\s*(-|\+)?((\.\d+)|(\d+\.\d+)|(\d+)|(\d+(\.\d+)?e(-|\+)?(\d+))))))\s*$/g))
121 throw new Error(`Attribute must be a comma- or space-separated sequence of up to three numbers, for example "1 2.5 3". Yours was "${str}".`)
122}
40function isNonEmptyStringArray( theArray ) {
41 var index;
42
43 if ( Array.isArray( theArray ) && theArray.length > 0 ) {
44 for ( index in theArray ) {
45 if ( typeof theArray[ index ] !== "string" ) {
46 return false;
47 }
48 }
49 return true;
50 }
51
52 return false;
53}
15export function isArrayOfStrings(value) {
16 return isArray(value) && value.every(isString);
17}
20public static isFilledStringArray(arr: any[]): boolean {
21 if (!arr || !Array.isArray(arr)) {
22 return false;
23 }
24 for (let element of arr) {
25 if (typeof element !== 'string' || element.trim() === '') {
26 return false;
27 }
28 }
29 return true;
30}
136function isStringArray(x: any): x is string[] {
137 return Array.isArray(x) && x.every(el => typeof el === 'string');
138}
8public isString(value: any) {
9 return typeof value === 'string' || value instanceof String;
10}
120private validateArray(input: StringInput, type: ArrayType): boolean {
121 const validateItems = () =>
122 !(input.value as [])
123 .map((v, index) =>
124 this.run(
125 { name: `${input.name}[${index}]`, value: `${v}` },
126 type.elementType
127 )
128 )
129 .some(v => v === false);
130
131 return Array.isArray(input.value) && validateItems();
132}
35public isEmpty(value: any) {
36 for (let key in value) {
37 if (value.hasOwnProperty(key)) {
38 return false;
39 }
40 }
41 return true;
42}

Related snippets