【发布时间】:2019-05-09 09:37:06
【问题描述】:
这是我正在处理的功能的一部分:
this.instructions 是一个数组。
我特意添加了一些 console.log() 只是为了告诉你我在 this.instructions 数组中寻找的项目存在。
insertOrUpdateI(i, instruction_frame) {
if (i.hasOwnProperty('src_id')) {
let iToUpdateIndex = this.instructions.findIndex(known_i => known_i.src_id == i.src_id);
console.log('i: ',i)
console.log('this.instructions: ',this.instructions);
console.log('iToUpdateIndex: ', iToUpdateIndex);
if (iToUpdateIndex > -1) {
let oldInstruction = this.instructions[iToUpdateIndex];
this.instructions[iToUpdateIndex] = {...i, frame: oldInstruction.frame}; // don't update the frame
this.reconstructMatchState({
fetchFromBack: false,
lastFrame: this.stream.lastCameraFrame,
applyNow: true
}).subscribe();
return
}
}
....
这是我得到的结果:
i : {subject: "card", player_id: 6547, card_type: "yellow_card", src_id: 1, frame: 5077}
this.instructions: [{subject: "period", name: "period_1", start: 5031, frame: 5031, src_id: -1}, {主题:“卡片”,player_id:6547,card_type:“yellow_card”,src_id:1,帧:5077}]
iToUpdateInde: -1
但是,我有 i.src_id === 1 并且我的数组中有一个项目的 src_id == 1
我错过了什么?我将不胜感激!
【问题讨论】:
-
iToUpdateIndex = this.instructions.indexOf(i)会更好。 -
@tricheriche 不会。
-
@JonasWilms 为什么不呢?
-
iToUpdateInde向我表明您显示的日志与您显示的 tue 代码不匹配。 minimal reproducible example 请。
标签: javascript angular typescript