Javascript数组查找检查未定义的
我正在使用对象数组列表中的javascript array.find。我只想得到该对象的另一个属性,当与该属性进行比较时 物业是可用的。 与t相比,我得到的值为'Value1' 与t1相比,我没有定义。我想检查“undefined”并仅在可用时获取值Javascript数组查找检查未定义的,javascript,arrays,object,ecmascript-6,find,Javascript,Arrays,Object,Ecmascript 6,Find,我正在使用对象数组列表中的javascript array.find。我只想得到该对象的另一个属性,当与该属性进行比较时 物业是可用的。 与t相比,我得到的值为'Value1' 与t1相比,我没有定义。我想检查“undefined”并仅在可用时获取值 const t = 'abc'; const t1= 'xyz'; temp = [ {key: "abc", value: "Value1}] temp.find( check => check.key === t ).value); 如
const t = 'abc';
const t1= 'xyz';
temp = [ {key: "abc", value: "Value1}]
temp.find( check => check.key === t ).value);
如果您希望它抛出错误、返回undefined、返回默认值或其他内容,您可以像这样检查undefined:
x = list.find(logic)
if(x){
//carry on as normal
else{
//handle it not being found
}
首先,你错过了最后的报价。其次,使用
some
和find
:
const t='abc';
常数t1='xyz';
const temp=[{key:“abc”,value:“Value1”}];
if(temp.some({key}=>key==t))console.log(temp.find({key}=>key==t.value)代码>根据您打算如何使用该值,您可能会发现搜索结果中需要某种类型的值。这将允许您根据需要替换某些内容或不替换任何内容
const t='abc';
常数t1='xyz';
temp=[{key:“abc”,value:“Value1”}]
const result=temp.find(check=>check.key==t1)| |{值:'notfound!';
console.log(result.value)
在Value1=>value:“Value1”}
之后,您似乎缺少一个双引号。
同时从temp.find(check=>check.key==t)的末尾删除额外的)
代码>我想在一个语句中执行此操作,而不是使用另一个变量(如“find”)进行检查。是否可以使用turnery运算符来代替使用某些变量