I want to check user.uid that contain or not in combined user ids. here is combined ids in arrays
getid Array [
"E8R52y6dD8XI3shXhnhS9pVzUpe26Lf5bHwzCSbI2bmoMv7KuduMGwe2",
"KgRwTDgenjYpODPxRaldDQy9nnH36Lf5bHwzCSbI2bmoMv7KuduMGwe2",
"pNv0iKlZ6xVOl0fFOZSoXJoPuVx2E8R52y6dD8XI3shXhnhS9pVzUpe2",
]
here is user.uid
pNv0iKlZ6xVOl0fFOZSoXJoPuVx2
I want to get result is my user.uid is in or not in this combined Id arrays.
const uid = user.uid in getId ? yes : no
(or ) how can check this condition .I not know. I not want to remove my user.uid from combined id , I just want to check my user.uid is in or not in this combined Id. can someone help me?
2 Answers 2
You can simply check using array functions like filter, find , findIndex, some
let array = [
"E8R52y6dD8XI3shXhnhS9pVzUpe26Lf5bHwzCSbI2bmoMv7KuduMGwe2",
"KgRwTDgenjYpODPxRaldDQy9nnH36Lf5bHwzCSbI2bmoMv7KuduMGwe2",
"pNv0iKlZ6xVOl0fFOZSoXJoPuVx2E8R52y6dD8XI3shXhnhS9pVzUpe2",
];
let userId = 'pNv0iKlZ6xVOl0fFOZSoXJoPuVx2';
const exist = array.filter(id => id.includes(userId)).length > 0;
//Using some method
const exist1 = array.some(id => id.includes(userId));
console.log(exist, exist1);
2 Comments
If you just want to get a boolean value indicating if element is in the array or not, you can simply use getId.includes(user.uid).
Check out this MDN link if want to know more about Array.prototype.includes()
const arr = [ "E8R52y6dD8XI3shXhnhS9pVzUpe26Lf5bHwzCSbI2bmoMv7KuduMGwe2", "KgRwTDgenjYpODPxRaldDQy9nnH36Lf5bHwzCSbI2bmoMv7KuduMGwe2", "pNv0iKlZ6xVOl0fFOZSoXJoPuVx2E8R52y6dD8XI3shXhnhS9pVzUpe2",
]
const invalidId = 'pNv0iKlZ6xVOl0fFOZSoXJoPuVx2'
const validId = "E8R52y6dD8XI3shXhnhS9pVzUpe26Lf5bHwzCSbI2bmoMv7KuduMGwe2"
const arrayHasInvalidId = arr.includes(invalidId)
const arrayHasValidId = arr.includes(validId)
console.log(arrayHasInvalidId,arrayHasValidId);