-1

How to make work findIndex depending of a snapshotkey ?it's not working with the variable but actually work with a manual string ?!

 this.state.data =     Object {

  "date_add": 1574177488383,
  "date_upd": 1574178141010,
  "question": "1",
"key": "123455566",
}
ref.on('child_changed', snapshot => {
          var key = snapshot.key


          console.log('the key', key) // return 123455566
})

actually not working like this :

      var foundIndex = this.state.data.findIndex(x => x.key == key); 

but work like this

 var foundIndex = this.state.data.findIndex(x => x.key == '123455566'); 
manyouuwx
  • 47
  • 6

2 Answers2

0
var key = "123456";
var data = [{"key":"123456"},{"key": "123"},{"key":  "12"}, {"key":"1"}]
var foundIndex = data.findIndex(x => x.key == key);
console.log(foundIndex); //0
var ind = data.findIndex(x => x.key == '12'); 
console.log(ind); //2
PragJh
  • 41
  • 1
  • 4
0

You should use:

var foundIndex = this.state.data.findIndex(x => x.key == x.key);

Im not sure what you are trying to get but, you usefindindex to get the index of a value.

You can see multiple examples here:

Get the index of the object inside an array, matching a condition

Find index of object in javascript by a value

Also the docs:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134