-1

I have an array named as joinedArray. In one case, it has the value [undefined, undefined].

I have written one if condition like:

if(joinArray === undefined){
    vm.selectedFriends = [];
    angular.forEach($scope.contacts, function(contact){
        if (contact.selected)
        vm.selectedFriends.push(contact.email);
    });

    $http({
        url: 'http://192.168.2.8:7200/api/creatList',
        method: 'POST',
        data: {wData:userData, uId:vm.uid, userName:vm.uName, email:vm.selectedFriends}
    }).success(function(res) {
        console.log("success");
    }, function(error) {
        console.log(error);
        alert('here');
    });
    $mdDialog.hide(userData);
} else {
    $http({
        url: 'http://192.168.2.8:7200/api/sendMail',
        method: 'POST',
        data: {wData:userData, email:joinArray, uId:vm.uid, userName:vm.uName}
    }).success(function(res) {
        console.log("success");
    }, function(error) {
        console.log(error);
        alert('here');
    });
    $mdDialog.hide(userData);
}

Sometimes joinedArray returns like [value, undefined] or [undefined, value]. But only when both values are undefined should it pass to the if condition, otherwise it should go to else condition.

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
Kevin
  • 653
  • 2
  • 13
  • 34
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every – Bergi Sep 23 '16 at 08:06
  • Possible duplicate of [Check if array is empty or exists](http://stackoverflow.com/questions/11743392/check-if-array-is-empty-or-exists) – Peter Morris Sep 23 '16 at 14:02

2 Answers2

1

Use Array.every...

var allAreUndefined = joinedArray.every(function(value) {
  return value === undefined;
});

To check if the array is defined you need to use typeof, as suggested in the answer to this question.

Community
  • 1
  • 1
Peter Morris
  • 20,174
  • 9
  • 81
  • 146
  • I have edited my question how to use for that if condition. – Kevin Sep 23 '16 at 08:09
  • I have used this before that if condition and I made if condition like if(joinArray === allAreUndefined) but its not working it is going to else condition. – Kevin Sep 23 '16 at 09:25
0

Use a stringified array, a regular expression replacement, and an empty string comparison. For example:

    /* If a stringified array consists of null values, convert it to an empty string, and return null
    */
    if (JSON.stringify(Array(10)).replace(/^\[(null[,\]])+/,"") === "")
      {
      console.log(null);
      }
    else
      {
      console.log(NaN);
      }

Or more specifically:

"use strict";

/* evaluate a stringified array to get the evaled values */
var foo = Function("return eval(JSON.stringify(Array(10)).replace(/null/g, undefined))")();


if (JSON.stringify(foo).replace(/^\[(null[,\]])+/,"") === "")
    {
    console.log("null");
    }
else
    {
    console.log("not null");
    }


/* 
Use foo.indexOf(null) before all this to avoid false positives
*/

References

Community
  • 1
  • 1
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265