I have two arrays like below:
const arr = [ 4, 5 ]
const arr2 = [
{
"id": 1
"type":[4]
},{
"id": 2
"type":[4,1]
},{
"id": 3
"type":[4,8,3]
},{
"id": 4
"type":[4,5]
}
]
how to sort arr2 in this way, that:
such that if the
typeofarr2contains all elements inarrthen those objects are at the very top of the list,when it has at least one element with
arr2then should be in the next position,and the last ones should be objects that only have one (the only one in
type) listed inarr,
so, the result should be:
const arr2 = [
{
"id": 4
"type":[4,5]
},{
"id": 2
"type":[4,1]
},{
"id": 3
"type":[4,8,3]
},{
"id": 1
"type":[4]
}
]
I try to do this with js` function like below:
arr2.sort(e => e.type.includes(arr))
but it not working correctly, can omeone tell me how to sort arr2 in the provided ways? thanks for any help!
sortfunction in Javascript, when called with a callback, expects a callback that takes two parameters and returns either -1, 0, or 1. It should return -1 if the first parameter should come before the second, 0 if the two parameters are equal, and 1 if the the first parameter should come after the second. This isn't a complete answer, but it is part of why your attempted solution does not work. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…