将选择范围缩小到可观察数组中的单个对象时,如何有效地获取该对象的索引?
@observable questionsList = [{
id: 1,
question: "Is the earth flats?",
answer: "Some long answer here..."
}, {
id: 2,
question: "Does the moon have life?",
answer: "Some long answer here..."
}];
const quesitonId = 2;
const question = this.questionsList.find(q => q.id === questionId);
const questionIndex = // should be 1
Run Code Online (Sandbox Code Playgroud)
Tho*_*lle 10
你可以使用findIndex:
questionsList.findIndex(q => q.id === 2);
Run Code Online (Sandbox Code Playgroud)
var questionsList = [{
id: 1,
question: "Is the earth flats?",
answer: "Some long answer here..."
}, {
id: 2,
question: "Does the moon have life?",
answer: "Some long answer here..."
}];
console.log(questionsList.findIndex(q => q.id === 2));Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1285 次 |
| 最近记录: |