Compare 2 arrays with some() or Every()

Asked

Viewed 28 times

0

I am trying to do an exercise that compares whether the array of objects inserted in a function has any element equal to the following array:

const gooUsers = [
    {id: 1},
    {id: 2},
    {id: 3}
];

The function input could be the following array as an example:

const test = [
    {id: 2},
    {id: 1}
];

The result in the above case would be true for some() and false for Every(), I believe.

The function I’ve been able to do so far:

const checkUsersValid = goodUsers => 
     (submittedUsers) =>{

         return goodUsers
            .some(value1=> submittedUsers
            .some(value2=> value1 === value2));
     }
  • What exactly is your problem?

1 answer

1


I don’t quite understand, but I believe that’s what you want,

goodUsers=[{id:1},{id:2},{id:3}];
const checkUsersValid= (submittedUsers) =>{

 return goodUsers
    .some(value1=> submittedUsers
    .some(value2=> Object.keys(value1)[0] === Object.keys(value2)[0] && Object.values(value1)[0] === Object.values(value2)[0]));
}
checkUsersValid([{id:1},{id:2}])

Browser other questions tagged

You are not signed in. Login or sign up in order to post.