快速提问。我可以用更短的方式写吗?
{this.state.members[activeMemberId].id ? this.state.members[activeMemberId].id : null}
例如
{this.state.members[activeMemberId].id ? <-the same : null}
答案 0 :(得分:4)
您可以这样做:
if ((byte & 0xE7) == 0x26) {
/* This `byte` is a ES, SS, CS or DS segment override prefix */
}
if ((byte & 0xFC) == 0x64) {
/* This `byte` is a FS, GS, Operand-size or address-size override prefix */
}
if ((byte & 0xFC) == 0xF0) {
if (byte != 0xF1) {
/* This `byte` is a LOCK, REPN(E/Z) or REP(_/E/Z) prefix */
}
}
答案 1 :(得分:1)
您可以使用逻辑OR
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.min.js"></script>
<script src="https://unpkg.com/vue-select@latest"></script>
<link rel="stylesheet" href="https://unpkg.com/vue-select@latest/dist/vue-select.css">
<div id="app">
<div>
<v-select v-model="selected" :options="options">
<template v-slot:option="option">
{{ option.id }} - {{ option.name }}
</template>
<template v-slot:selected-option="option">
{{ option.id }} - {{ option.name }}
</template>
</v-select>
<div v-if="selected !== ''" style="margin-top: 20px;">
<hr/> Selected Item:
<pre>{{ selected }}</pre>
<hr/>
</div>
<div v-if="selected !== ''" style="margin-top:20px;">
Name:
<div>
<input type="text" v-model="selected.name" />
</div>
ID:
<div>
<input type="text" v-model="selected.id" />
</div>
<hr/>
</div>
</div>
</div>
这是如何工作的?
{ this.state.members[activeMemberId].id || null }
拇指规则:- 逻辑或返回第一个真值,如果还有其他则返回最后一个值
答案 2 :(得分:0)
您可以使用||
this.state.members[activeMemberId].id || null
答案 3 :(得分:0)
请小心不要试图访问undefined
的id属性时得到未定义的引用异常
function getMemberId(activeMemberId) {
this.state = { members: {
id1: { id: 1 }
} };
const { members: { [activeMemberId]: { id = null } = {} } = {} } = this.state;
return id;
}
console.log(getMemberId('id1'));
console.log(getMemberId('id2'));
// Or you could chain the && operator and || operator
function getMemberIdAlt(activeMemberId) {
this.state = { members: {
id1: { id: 1 }
} };
return (
this.state
&& this.state.members
&& this.state.members[activeMemberId]
&& this.state.members[activeMemberId].id
) || null;
}
console.log(getMemberIdAlt('id1'));
console.log(getMemberIdAlt('id2'));