简体   繁体   中英

findIndex() returns -1 even though item exists in the Array

Here is a part of the function I'm working on:

this.instructions is an array.

I've deliberately added some console.log() just to show you that the item I'm looking for in this.instructions array exists.

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
      }
    }
....

Here is the result I get:

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}, {subject: "card", player_id: 6547, card_type: "yellow_card", src_id: 1, frame: 5077}]

iToUpdateInde: -1

However, I have i.src_id === 1 and there is an item on my array that has a src_id == 1

What am I missing? I would appreciate some help!

这对我来说是一个stackblitz的例子 ,尝试做另一个服务。

Finally, I've found an answer to my question here: Is console.log async or sync There was nothing wrong with my code, the problem may have come from the version of developer tools I was using.

It seems that console.log() have different behaviors according to the browser we're using and it's version. And sometimes it may have an asynchronous behavior, even though we aren't passing any callbacks inside of it!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM