I have the following and need to test if a user id is present anywhere in the collection.
const meetingCollection = [
{ id: "2", userIds: [12, 34, 55, 6, 7, 43543, 45345, 545] },
{ id: "21", userIds: [3, 425, 5, 33, 7, 68, 76, 99, 66, 99] },
{ id: "22", userIds: [767, 5654756, 5658, 86, 45, 23456, 666] },
{ id: "23", userIds: [6, 43562, 5645747, 4654, 2577, 4345, 45777] },
{ id: "24", userIds: [45645, 124, 4, 435, 6, 7755, 7, 8] },
{ id: "25", userIds: [1, 2, 455, 647, 753] },
]
const queryString = 7755;
meetingCollection.some(m => m.userIds.some(userId => userId === queryString))
Array.prototype.some() tests the method against each “row” of the collection and returns true on the first hit and then stops. If you have a 1000 elements and the method returns true on the 10th iteration .some() stops looping over the collection and returns true.
If a method fails to return true then false is returned.
The above example I’ve nested .some()
Try this in the console….
const meetingCollection = [
{ id: "2", userIds: [12, 34, 55, 6, 7, 43543, 45345, 545] },
{ id: "21", userIds: [3, 425, 5, 33, 7, 68, 76, 99, 66, 99] },
{ id: "22", userIds: [767, 5654756, 5658, 86, 45, 23456, 666] },
{ id: "23", userIds: [6, 43562, 5645747, 4654, 2577, 4345, 45777] },
{ id: "24", userIds: [45645, 124, 4, 435, 6, 7755, 7, 8] },
{ id: "25", userIds: [1, 2, 455, 647, 753] },
]
const findId = (queryString) => {
return meetingCollection.some(m => m.userIds.some(userId => userId === queryString))
}
console.log(findId(2577)); // true
console.log(findId(257)); // false