Skip to content Skip to sidebar Skip to footer

How To Find Array On Mongoose Doc Object?

I interest How to find array in mongoose. for example: if I have this data: [ { nums: [2,3,1,4,5] }, { nums: [2,3] }, { nums: [1,2,3] }, { nums: [2,3,1,4,5,10,9] },

Solution 1:

Use Array.some to check whether the nums property of an item in the array is equal to the array you're looking for:

const arr = [
   { nums: [2,3,1,4,5] },
   { nums: [2,3] },
   { nums: [1,2,3] },
   { nums: [2,3,1,4,5,10,9] },
]

const lookFor = [1,2,3,4,5].sort().toString()
const contains = arr.some(e => e.nums.sort().toString() == lookFor);
console.log(contains);

Post a Comment for "How To Find Array On Mongoose Doc Object?"