[英]How can I return a property value for the first element in an array or 0 if there are no elements?
In my HTML I have this code block: 在我的HTML中,我有以下代码块:
<div ng-click="wos.wordWordFormRowClicked(wf)"
ng-repeat="wf in word.wordForms">
...
</div>
The word
object can have 0 or many wordForms
and each wordForm
has a property wordFormIdentityId
. word
对象可以具有0个或多个wordForms
,每个wordForm
都有一个属性wordFormIdentityId
。
In javascript how can I get the wordFormIdentityId
of the first wordForm
or return 0 if there are no wordForms. 在JavaScript我如何能得到wordFormIdentityId
第一wordForm
或返回0,如果没有词形等。 Hope this makes sense, if not please ask and I will try to explain more. 希望这是有道理的,如果不能,请提出询问,我将尽力解释更多。
Here are the typescript interfaces: 这是打字稿界面:
interface IWord {
wordForms: IWordForm[];
wordId: string;
}
interface IWordForm {
definition: string;
wordFormId: string;
wordFormIdentity: number;
wordId: string;
}
Am I missing something or does this handle it? 我是否缺少某些东西或可以处理? I responded in Typescript as well. 我也以Typescript回应。
function getFirstId (word:IWord) :number {
if(word && word.wordForms && word.wordForms.length > 0) {
return word.wordForms[0].wordFormIdentity;
}
return 0;
}
I think that's what you need in your function: 我认为这就是您需要的功能:
if(word.wordForms)
return word.wordForms[0].wordFormIdentity || 0;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.