Checking and removing duplicates of the Parse.com class

I have a class in parse called testItem, this is a snapshot of this class

enter image description here

As you can see, the same element appears many times, but this is normal because it is in different schools and canteens, but this item appears twice in the same dining room and school, so this is a duplicate in diningHallNumber: 1 and Union College twice, so this is a duplicate. Therefore, I am trying to write a code function to remove it. Here is what I still have:

Parse.Cloud.job("removeDuplicateItems", function(request, response) { function checkDuplicate(school) { var TestItem = Parse.Object.extend("TestItem"); var testItemsQuery = new Parse.Query(TestItem); testItemsQuery.equalTo('school', schoolArray[i]); testItemsQuery.each(function(testItem) { var item = testItem.get('item'); var school = testItem.get('school'); var diningHallNum = testItem.get('diningHallNumber'); var testItemsQueryCheck = new Parse.Query(TestItem); testItemsQueryCheck.equalTo ('item', item); testItemsQueryCheck.equalTo ('school', school); testItemsQueryCheck.equalTo ('diningHallNumber', diningHallNum); //then delete Item } var schoolArray = ['Union College (NY)', 'University of Albany', 'Rensselaer Polytechnic Institute']; for (var i = 0; i < schoolArray.length; i++) { checkDuplicate(schoolArray[i]); } } 

but it will not work, because it will always come true. I need a way to see if this is the second time this item has appeared. How can i do this?

Thanks for the help in advance.

+5
source share
1 answer

You can use a hash table to track duplicate elements. Sort of:

 Parse.Cloud.job("removeDuplicateItems", function(request, status) { Parse.Cloud.useMasterKey(); var _ = require("underscore"); var hashTable = {}; function hashKeyForTestItem(testItem) { var fields = ["item", "meal", "schoolMenu", "diningHallNumber", "school"]; var hashKey = ""; _.each(fields, function (field) { hashKey += testItem.get(field) + "/" ; }); return hashKey; } var testItemsQuery = new Parse.Query("TestItem"); testItemsQuery.each(function (testItem) { var key = hashKeyForTestItem(testItem); if (key in hashTable) { // this item was seen before, so destroy this return testItem.destroy(); } else { // it is not in the hashTable, so keep it hashTable[key] = 1; } }).then(function() { status.success("Migration completed successfully."); }, function(error) { status.error("Uh oh, something went wrong."); }); }); 
+11
source

Source: https://habr.com/ru/post/1204357/


All Articles