2014-10-09 18 views
5

Mam klasy w parse nazywa testItem tutaj jest bardzo prosta strzał z tej klasysprawdzanie i usuwanie duplikatów Parse.com klasy

enter image description here

Jak widać ta sama pozycja pojawia się wiele czasu, ale jest dobrze, bo jest w różnych szkołach i salach restauracyjnych, ale ten przedmiot pojawia się dwa razy w tej samej restauracji Hall i szkoła, więc jest to duplikat, Oven Roast Potates jest w diningHallNumber: 1 i Union College dwa razy, więc jest duplikatem. Próbuję więc napisać funkcję kodu, aby ją usunąć. Oto co mam do tej pory:

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]); 
} 
} 

ale to nie działa, bo to zawsze się spełniają Potrzebuję sposób, aby zobaczyć, czy jest to drugi raz ten artykuł ma pochodzić. Jak miałbym to zrobić?

Dzięki za pomoc z góry !!!

+0

http://stackoverflow.com/questions/20880781/parse-com-find-all-objects-belonging-to-a-user-with-objectid –

+0

@VitorinoFernandes nie jestem pewien, w jaki sposób to w pełni odnosi się do tego, o co pytam: –

+0

@VitorinoFernandes Im bardziej próbuję sprawdzić, czy jest duplikat, i go usunąć. –

Odpowiedz

11

Możesz użyć tabeli mieszania do śledzenia duplikatów. Coś jak:

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."); 
    }); 
}); 
+0

Działa to świetnie, Czy istnieje sposób na zachowanie najstarszego duplikatu? –

Powiązane problemy