admin管理员组文章数量:1332383
When narrowing down a selection to a single object in an observable array, how can I get that object's index efficiently?
@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
When narrowing down a selection to a single object in an observable array, how can I get that object's index efficiently?
@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
Share
edited Sep 19, 2016 at 18:04
Tholle
113k22 gold badges208 silver badges197 bronze badges
asked Sep 19, 2016 at 17:58
WonkaWonka
8,68422 gold badges82 silver badges126 bronze badges
1 Answer
Reset to default 11You could use the findIndex:
questionsList.findIndex(q => q.id === 2);
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));
本文标签: javascriptMobXRetrieve index of observable array objectStack Overflow
版权声明:本文标题:javascript - MobX - Retrieve index of observable array object? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742296331a2448802.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论