简体   繁体   English

如何使用数组splice()函数删除对象中的子数组?

[英]How do I use the array splice() function to remove a sub array in an Object?

I have written a code that successfully inserts an array (called: "wishlistArray") into an existing Object using the array push() function. 我编写了一个代码,该代码使用数组push()函数成功地将数组(called: "wishlistArray")插入到现有的Object中。

As you can already guess, its triggered when a user wants to add an item to his/her Wish list . 您已经猜到了,它在用户要将商品添加到他的“ 愿望清单”时触发。

Find below my Object architecture for clarity, that indicates that a user with user ID: "udEnfEmy5DSBvDsSy" has already added the Object to his/her "Wish List" 为了清楚起见,请在我的对象体系结构下面查找,这表明具有用户ID: “ udEnfEmy5DSBvDsSy”的用户已经将该对象添加到他/她的“愿望清单”中

_id: "DFhcAYAtMBviczogo"
createdDate: "04/12/2017"
expiryDate: "04/22/2017"
noOfViews: 125

   wishListArray: Array[1]
   0: Object
      wishedBy: "udEnfEmy5DSBvDsSy"

To avoid inserting duplicates in the wishlistArray array, I check if the current user ID already exists in the wishlistArray array, as per code below: 为了避免wishlistArray阵列中插入重复的,我检查当前用户ID wishlistArray阵列中已经存在,如下面的代码:

var selectedItemIDSet = Session.get('selectedItemIDSet');
var addToWishList = buyList.find(selectedItemIDSet);
var currentUser = [Meteor.user()._id ];

var wheatherWishListedOrNot = buyList.find({ _id:selectedID, 'wishListArray.wishedBy': {$in: currentUser } } ).count();

addToWishList.forEach(function(itemName){

 var currentObjectID = itemName._id; 

        //### If wishListArrayArray array already exists in then...
        if (itemName.wishListArray) {

               var wishListArray = [,...itemName.wishListArray];

                    //### If current user hasnt wish listed item then add to Wish List (wishListArray) array
                if (wheatherWishListedOrNot == "0") { 

                        wishListArray.push({ wishedBy: Meteor.user()._id });
                        Session.set('wishListArrayToGo',wishListArray );

                        }
                else if(wheatherWishListedOrNot == "1") {
                        //### Else if WishedListed by current user, Do absolutely nothing!

                        }   
                }                                  

        var wishListArray = Session.get('wishListArrayToGo');  

        buyList.update(currentObjectID, {$set: {wishListArray: wishListArray} }); 

    });    

Having explained how I've achieved this, I am having issues doing the reverse? 解释了我是如何做到的之后,我在做相反的事情时遇到了问题? How do I remove the user from the wishListArray array? 如何从wishListArray数组中删除用户?

I have tried the code below without any success. 我尝试了下面的代码,但没有成功。

var selectedItemIDSet = Session.get('selectedItemIDSet');
var removeFromWishList = buyList.find(selectedItemIDSet);

var currentUser = [Meteor.user()._id ];

wheatherWishListedOrNot = buyList.find({ _id:selectedID, 'wishListArray.wishedBy': {$in: currentUser } } ).count();


removeFromWishList.forEach(function(itemName){

 var currentObjectID = itemName._id; 

        //### If wishListArrayArray array already exists in then...
        if (itemName.wishListArray) {

               var wishListArray = [,...itemName.wishListArray];

                    //### If current user current user is on **WishedListArray**, then remove from **WishedListArray** array.
                if (wheatherWishListedOrNot == "1") { 

                        wishListArray.splice({ wishedBy: Meteor.user()._id });
                        Session.set('wishListArrayToGo',wishListArray );

                        }
                else if(wheatherWishListedOrNot == "0") {
                        //### Else if current user isnt on WishedListArray, Do absolutely nothing!

                        }   
                }                                  

        var wishListArray = Session.get('wishListArrayToGo');  

        buyList.update(currentObjectID, {$set: {wishListArray: wishListArray} }); 

    });     

The code where I attempt to remove seems logical to me, but fails to remove the user id from the WishlistArray . 我尝试删除的代码对我来说似乎很合逻辑,但是无法从WishlistArray中删除用户ID。 Can someone kindly point out where I am going wrong? 有人可以指出我要去哪里了吗?

Thanks in advance! 提前致谢!

You could achieve what you want using simple mongo operators. 您可以使用简单的mongo运算符来实现所需的功能。 To make it even simpler you can use an array of Strings as wishList (The userIds). 为了使其更简单,您可以使用Strings数组作为wishListwishList )。

To insert a user: 插入用户:

buyList.update(currentObjectID, { $addToSet: { wishList: userId} });

addToSet will only insert the userId if not already in there. addToSet将仅插入userId如果尚未插入)。

and to remove it again: 并再次将其删除:

buyList.update(currentObjectID, { $pull: { whishList: userId } });

How about it? 这个怎么样?

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

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