Home > front end >  Array.some() not pushing into empty array
Array.some() not pushing into empty array

Time:10-02

I have a function that takes two arrays. both arrays are simply arrays of strings. each string is unique Id. I am doing array.filter and for each element in the array im doing secondArray.some() to see if any of the elements in the first array are contained in the second array. If not, I am pushing those values into a new array. This is working when my secondArray has values, but if my second Array is empty, I cannot push into new array

Here is my function below

const group_contacts = [
"614c38608b931604b", // the contacts contained within the group we send over
"614c39598b931604b",
//"614c39ac8b931604",
//"614d3dab1c6c37220"
]

const selectedContacts = [ // the selected contacts to import into the group
    "61380eeda0817b22c",
    "61380f14a0817b22c",
    "614c38608b931604b",
    "614c39598b931604b"
]
 const addContactsToGroup = async (req, res) => {
                const { groupId, selectedContacts } = req.body
                let Matches = []
               const group = await Group.findById(groupId) // this group contains contacts array that I will push final result into
               console.log(group)
                selectedContacts.filter(contact => {
                    console.log(contact)
                    group_contacts.some(groupContacts => { // this is actually group.contacts in my real function, just using group_contacts and providing array above to simplify
                        console.log(groupContacts)
                        if(groupContacts === contact) {
                            console.log(groupContacts) // basically do nothing
                        }
                        Matches.push(groupContacts) // if group doesnt have matching contact, then push into Matches array
                    })
                })
                console.log(Matches)
                const unique = [...new Set(Matches)] // clean the array of duplicates
                console.log(unique)
            }

CodePudding user response:

As described in MDN about Array.some(), it returns true / false:

The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array.

So if you want to loop over your second array and check its existence in the first array, you can use some to check if the contact exists in the second array or not, using the below logic:

const matches = []
selectedContacts.filter(contact => {
  if (!group_contacts.some(groupContact => groupContact === contact)) {
      matches.push(contact)
  }
})

CodePudding user response:

You have a logical issue in your code.

Array.some definition. Reference

The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array.

Array.some is basically a method which is suposed to return true or false id a node is present in an array. You are trying to preform a looping logic using Array.some.

You have to just loop throgh your selectedContacts array and check whether any nodes from this array is not matching with nodes in group_contacts. So a Array.forEach and Array.some will do it for you.

Array.forEach implementation

const group_contacts = [
  "614c38608b931604b", // the contacts contained within the group we send over
  "614c39598b931604b",
  //"614c39ac8b931604",
  //"614d3dab1c6c37220"
]

const selectedContacts = [ // the selected contacts to import into the group
  "61380eeda0817b22c",
  "61380f14a0817b22c",
  "614c38608b931604b",
  "614c39598b931604b"
];
const Matches = [];
selectedContacts.forEach(contact => {
  const isMatched = group_contacts.some(groupContacts => groupContacts === contact);
  if (!isMatched) {
    Matches.push(contact)
  }
});
console.log(Matches);

You can achive this using any other looping logic aswell. Array.map will not suit here. Because Array.map is supposed to retun a node against each node from the calling array.

You can also make use of Array.filter. You just wants to filter out data from selectedContacts, which are not present in group_contacts. So your filnal response with Array.filter will be.

const group_contacts = [
  "614c38608b931604b", // the contacts contained within the group we send over
  "614c39598b931604b",
  //"614c39ac8b931604",
  //"614d3dab1c6c37220"
]

const selectedContacts = [ // the selected contacts to import into the group
  "61380eeda0817b22c",
  "61380f14a0817b22c",
  "614c38608b931604b",
  "614c39598b931604b"
];

const Matches =  selectedContacts.filter((contact) => {
  const isMatched = group_contacts.some(groupContacts => groupContacts === contact);
  return !isMatched;
});
console.log(Matches)

Array.reduce can also give a helping hand.

Array.reduce implementation

const group_contacts = [
  "614c38608b931604b", // the contacts contained within the group we send over
  "614c39598b931604b",
  //"614c39ac8b931604",
  //"614d3dab1c6c37220"
];
const selectedContacts = [ // the selected contacts to import into the group
  "61380eeda0817b22c",
  "61380f14a0817b22c",
  "614c38608b931604b",
  "614c39598b931604b"
];
const Matches =  selectedContacts.reduce((acc, contact) => {
  const isMatched = group_contacts.some(groupContacts => groupContacts === contact);
  if (!isMatched) {
    acc.push(contact)
  }
  return acc;
}, []);
console.log(Matches);

  • Related