admin管理员组

文章数量:1312839

I have a class in parse called testItem here is a snap shot of this class

As you can see the same item es up multiple time but that is fine because it is at different schools and dining halls, but this item es up twice at the same diningHallNumber and school so it is a duplicate, Oven Roast Potates is at diningHallNumber: 1 and Union College twice so it is a duplicate. So I am trying to write a could code function to remove this. Here is what I have so far:

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 this doesn't work because it will always e true I need a way to see if this is the second time this item has e up. How would I go about doing this?

Thanks for the help in advance!!!

I have a class in parse called testItem here is a snap shot of this class

As you can see the same item es up multiple time but that is fine because it is at different schools and dining halls, but this item es up twice at the same diningHallNumber and school so it is a duplicate, Oven Roast Potates is at diningHallNumber: 1 and Union College twice so it is a duplicate. So I am trying to write a could code function to remove this. Here is what I have so far:

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 this doesn't work because it will always e true I need a way to see if this is the second time this item has e up. How would I go about doing this?

Thanks for the help in advance!!!

Share Improve this question edited Oct 9, 2014 at 23:10 asked Oct 9, 2014 at 18:31 user3692917user3692917 5
  • stackoverflow./questions/20880781/… – Vitorino fernandes Commented Oct 9, 2014 at 18:48
  • @VitorinoFernandes im not quite sure how this fully relates to what I am asking – user3692917 Commented Oct 9, 2014 at 18:50
  • @VitorinoFernandes Im more trying to check if there is a duplicate, and remove it. – user3692917 Commented Oct 9, 2014 at 19:06
  • the duplicate entries are 2nd and 3rd right ? you want to delete one and keep one? – Vitorino fernandes Commented Oct 12, 2014 at 15:32
  • @VitorinoFernandes right i only want to delete the duplicate and keep the others – user3692917 Commented Oct 13, 2014 at 14:31
Add a ment  | 

1 Answer 1

Reset to default 11 +100

You can use a hash table to keep track of the duplicate items. Something like:

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 pleted successfully.");
  }, function(error) {
    status.error("Uh oh, something went wrong.");
  });
});

本文标签: javascriptCheck and delete duplicates Parsecom classStack Overflow