const array =
[
[1,2,3],
[4,1,1,3]
[5,5,7]
]
const newArray = [2,3,1]
How I can find with javascript if this array exists in the array (exists with other order)
I sloved this by sort the array
and check with JSON.stringify
let conflictIds = [...conflictsList.map(c=>c._id), ssdst._id]
conflictIds = conflictIds.sort();
if(!existsInArray(conflictsArr, conflictIds)){
const conflictObj = {schedules:conflictIds .... }
conflictsArr.push(conflictObj)
}
const existsInArray = (array, newElement)=>{
return array.find(arr=>JSON.stringify(arr.schedules) ===JSON.stringify(newElement) )
}
3
Answers
You can calculate the frequencies of elements in each array and compare them.
"array in array" means that the second array could be bigger. To find the result we need collect counts of values and then substract counts from the first array. If there’s any negative count value that means that the second array doesn’t contain all values to recreate the first array: