简体   繁体   中英

How to remove object element that match from an array using splice?

here is my data of array1 :

[ { members: [ '60ee9148104cc81bec3b97ab' ] } ]

and here is array2:

[{"_id": "60ee9148104cc81bec3b97ab","username": "user1", "email": "user1@gmail.com"}, {"_id": "60ee917f767bd11d687326c7","username": "user2","email": "user2@gmail.com"}]

and I want to remove the object from my array2 which _id is equal to 60ee9148104cc81bec3b97ab

I have tried so far as

    let user = await User.find({ _id: { $ne: req.user._id } })

        const getNonfriends = (one) => {
            user.splice(user.indexOf(one.members[0]), 1)
           //user.filter(entry => entry._id !== one.members[0])
        }

   array1.map(getNonfriends)

filter or splice non of them bring my solutions.

We can use the Array.prototype.filter() I think there is no need to find the index of the array and slice.
Also, we can use the Array.prototype.map, it is similar to use the filter function

 const obj1 = [{ members: ['60ee9148104cc81bec3b97ab'] }] const obj2 = [{ "_id": "60ee9148104cc81bec3b97ab", "username": "user1", "email": "user1@gmail.com" }, { "_id": "60ee917f767bd11d687326c7", "username": "user2", "email": "user2@gmail.com" }] const getAnswer = (obj2) => { const res = obj2.filter(item => !obj1[0].members.includes(item._id)) return res; } console.log(getAnswer(obj2));

The mistake you make is that you have similar objects, yet you search for identical objects.

var array1 = [ { members: [ '60ee9148104cc81bec3b97ab' ] } ];
var array2 = [{"_id": "60ee9148104cc81bec3b97ab","username": "user1", "email": "user1@gmail.com"}, {"_id": "60ee917f767bd11d687326c7","username": "user2","email": "user2@gmail.com"}]

    for (var x of array1) {
        for (var member of x.members) {
            var objects = array2.filter(item => item._id === member)
            for (var obj of objects) array2.splice(array2.indexOf(obj), 1)
        }
    }

As you are working with IDs, I am going to assume that the second array cannot contain two items with the same ID. If that assumption is correct, you could do:

 const data = [{ "_id": "60ee9148104cc81bec3b97ab", "username": "user1", "email": "user1@gmail.com" }, { "_id": "60ee917f767bd11d687326c7", "username": "user2", "email": "user2@gmail.com" }]; const filter = [{ members: ['60ee9148104cc81bec3b97ab'] }]; // SOLUTION 1: using Array.prototype.findIndex() and Array.prototype.splice() const filteredDataA = [...data] // as splice() modifies the original array, I think it is safer to work on a copy of the original data filter[0].members.forEach(id => { const index = filteredDataA.findIndex(item => item._id === id); // find the index of the item with the same ID if (index > -1) filteredDataA.splice(index, 1); // (if found) remove the item }) console.log('SOLUTION 1: using Array.prototype.findIndex() and Array.prototype.splice()'); console.log(filteredDataA); console.log('--------------------'); // SOLUTION 2: using array.prototype.filter() and array.prototype.includes() const filteredDataB = data.filter(item => !filter[0].members.includes(item._id)); console.log('SOLUTION 2: using array.prototype.filter() and array.prototype.includes()'); console.log(filteredDataB); console.log('--------------------');

I'd prefer "Solution 2" as I think is more readable

From the answers, I got the clue and this brings my desire results. here is one thing that array1 can have multiple elements so it needs to be mapped over with invoking the getNonfriends finction. so,

let user = await User.find({ _id: { $ne: req.user._id } })
const getNonfriends = (one) => {
           user = user.filter(item => !one.members.includes(item._id))
           return user;
        }
await array1.map(getNonfriends)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM