nodejs check if array has at least 1 value

Solutions on MaxInterview for nodejs check if array has at least 1 value by the best coders in the world

showing results for - "nodejs check if array has at least 1 value"
Santino
19 Feb 2020
1/**
2 * This function checks if the passed param is an array and if it has at least 1 element.
3 * 
4 * @name exists
5 * @param {array} input Likely an array but possible for other data types, the data that needs to be checked.
6 * @return {boolean} true: input param is an array and has at least 1 element, false: input param is not an array or is an array but with no elements.
7 */
8
9Array.exists = input => { return Array.isArray(input) && input.some(e => e) }
10
11// Usage
12
13Array.exists([]) // false
14Array.exists(`Hello World`)// false
15Array.exists([`Hello World`]) // true