2017-02-13 188 views
1

我在我的角度工厂服务中有一个函数来获取一些数据。在使用对象之前,如何检查对象内是否存在某个值?如何检查在JavaScript中的对象中是否存在值

这里是我一直在努力...

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

,所以我只是想检查是否有在app.subject.name值在restanguar通话使用它之​​前...

感谢

回答

2

您的代码将检索length属性的值并尝试将其转换为布尔值用于if/then测试的目的,但是如果值恰好为,则会引发错误0。此外,如果您的测试仅仅是:app.subject.name,如果该值恰巧是一个虚假值,如0false,那么这两个值都是完全有效的值,您将得到误报。

对于字符串,最简单的测试是检查非空字符串和非空字符串。如果值是由最终用户提供的,则最好先在字符串上调用.trim()以删除可能已无意添加的任何前导或尾随空格。

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."); 
 
}

而且,如果该值是存在的,把你的代码在if真的分支不用担心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()}); 
    } 
+2

但是0的长度属性是虚假的,所以'!object.length'只是一个用于检查它是否为'> 0'的短代码。 – Connum

+0

这也会在'name' doesn的情况下抛出'undefined'' TypeError' 'app.subj上不存在'等等。 – Pineda

+1

当然会。只要您检查一个不存在的属性,就会发生这种情况。这就像是说'sdlfj = 894#$ @ $%$。[] {} {'会抛出语法错误。 –

1

的hasOwnProperty()方法返回布尔值,指示该对象是否具有指定的属性。 MDN Docs

var priceOfFood = { 
    pizza: 14, 
    burger 10 
} 

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

这是如何帮助检查一个属性是否有价值。问题不在于如何检查物业是否存在。 –

+0

这是一个类比的解释。我认为OP不是要检查价值,而是要求关键。 –

+2

OP非常清楚地说:*“所以我只是想检查它在app.subject.name中的价值,然后在restanguar调用中使用它”* –

0

就这么简单:

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

正如我在其他几个答案中提到的,这会抛出如果该值是一个像0或false这样的虚假值,这两个值都是有效值,则为假。 –

+0

它不会进入'if'语句,因为输入值是一个字符串,因为表达式'0'或'false'在'if'语句中为'true' – Korte

+0

这取决于如何写入测试。 'if(“0”== true){}'产生'false',而if(“0”){}'产生'true'。 –

1

我知道这个问题不问Lodash,但我能做到这种检查了很多与它和它完美无瑕。在你的情况下,它会是这样的:

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

如果用户希望按键可能无法使用应用对象内部,你可以做这样的:

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

或者干脆:

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

如果'app.subject.name ===“”''怎么办? –

+1

你的“完美”答案有一个缺陷:https://plnkr.co/edit/2sRu0U4hbOIdVnayW5EP –

+0

我没有看到任何问题,你可以重新检查它'_.isEmpty(data.career.field)'返回true为它应该是。 – Shota

相关问题