简体   繁体   中英

How to check if value exists in an object in JavaScript

I have a function in my angular factory service to fetch some data. How do I check that a certain value exists inside the object before using it?

here is what I've been trying...

categories.fetch = function(app){
  if(!app.subject.name.length){
    return false;
  }
  var p = Restangular.all('v1/categories').getList({app.subject.name.toUpperCase()});
}

so I just want to check that there is value in app.subject.name before using it in the restanguar call...

Thanks

Your code will retrieve the value of the length property and attempt to convert it to a Boolean for the purposes of the if/then test, but this will throw an error if the value happens to be null .

Also, if your test is simply: app.subject.name , you will get a false positive if the value happens to be a falsy value, like 0 or false , which are both perfectly valid values.

With strings, the simplest test is to check for a non-empty string and non-null. If the value was provided by an end-user, it's a good idea to call .trim() on the string first to remove any leading or trailing spaces that may have been inadvertently added.

 var myObj = { test : 0, testing : null } // This will fail with an error when the value is null /* if(myObj.testing.length){ console.log("The testing property has a value."); } else { console.log("The testing property doesn't have a value."); } */ // This will return a false positive when the value is falsy if(myObj.test){ console.log("The test property has a value."); } else { console.log("The test property doesn't have a value."); // <-- Incorretly reports this } // This explicit test will pass and fail correctly if(myObj.testing !== "" && myObj.testing !== null){ console.log("The testing property has a value."); } else { console.log("The testing property doesn't have a value."); } 

Also, if the value is there, put your code in the if s true branch and don't worry about return false .

categories.fetch = function(app){
  if(app.subject.name !== "" && app.subject.name !== null) {
    var p = 
      Restangular.all('v1/categories').getList({app.subject.name.toUpperCase()});
  }

The hasOwnProperty() method returns a boolean indicating whether the object has the specified property. MDN Docs

Example

var priceOfFood = {
    pizza: 14,
    burger 10
}

priceOfFood.hasOwnProperty('pizza') // true
priceOfFood['pizza'] // 14
priceOfFood.hasOwnProperty('chips') // false
priceOfFood['chips'] // undefined

I know the question does not ask about Lodash, but I manage to do this kind of checking a lot with it and it works flawlessly. In your case it would be something like this:

categories.fetch = function(app){
  if (_.isEmpty(app.subject.name)) {
   return false;
  }
 var p = Restangular.all('v1/categories').getList({app.subject.name.toUpperCase()});
}

If you expect that keys may not be available inside app object you can do it like this:

categories.fetch = function(app){
  if (_.isEmpty(_.get(app, "subject.name"))) {
   return false;
  }
 var p = Restangular.all('v1/categories').getList({app.subject.name.toUpperCase()});
}

Or simply:

categories.fetch = function(app){
  if (!_.get(app, "subject.name")) {
   return false;
  }
 var p = Restangular.all('v1/categories').getList({app.subject.name.toUpperCase()});
}

Simple as that:

if(!app.subject.name){
        return ;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM