Node.js: Checking if an Array is Empty using Javascript

To determine if all or none of the elements are filled, you can make two calls. If neither condition is met, the result will be false. There are different ways to check if a JavaScript object is empty, including using ES2015 or pre-ES2015 styles. An alternative solution is to use the fact that an empty req.body returns an empty object, which can be tested for fullness using the !arr.length method commonly used for empty arrays.


Solution 1:

It appears that you are seeking the utilization of the array’s

.length

feature.

let response = 
{
    "test1": [],
    "test2": [],
    "test3": [],
    "test4": null,
    "test5": []
}

if(response){
    if(response.test1.length == 0)
    {

      console.log("array empty");
    }
}


Solution 2:


Utilize the property “length” of arrays in JavaScript.

  if(test1.length){
  //if array is not empty do something here
 }
 else{
       //array is empty..do something else
  {


Solution 3:

you should run something like this

if (response){
    if (response["test1"] && response["test1"].length>0){
        //do stuff
    }
    if (response["test2"] && response["test2"].length>0){
        //do stuff
    }
    ...
}

Frequently Asked Questions