简体   繁体   English

如何使用拼接从数组中删除匹配的对象元素?

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

here is my data of array1 :这是我的 array1 数据:

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

and here is array2:这是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我想从我的 array2 中删除 _id 等于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.我们可以使用 Array.prototype.filter() 我认为不需要找到数组和切片的索引。
Also, we can use the Array.prototype.map, it is similar to use the filter function另外,我们可以使用Array.prototype.map,它类似于使用过滤器功能

 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.当您使用 ID 时,我将假设第二个数组不能包含两个具有相同 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我更喜欢“解决方案 2”,因为我认为它更具可读性

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.有一件事是 array1 可以有多个元素,因此需要通过调用 getNonfriends 函数来映射它。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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