I have two arrays, i want to push some items from arr1 to arr2 preventing duplicates because i've already added some items to arr2 that are also in arr1. i am doing this in react js
arr1.forEach(q1 => {
arr2.forEach(q2 => {
if (q1.lessonId !== q2.lessonId) {
if (q2.obtainedMarks >= q2.passingMarks) {
User.findByIdAndUpdate(req.body.userId, { $push: { arr1: { $each:
[q2._id] } } }, (err, doc) => {
})
}
}
})
});
You can use a set instead of an array. They have the built in feature of preventing duplicate entries. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With