Loop, get unique values ​​and update

I do below to get specific nodes from a tree, followed by getting text from these nodes, filtering the text to remove unique ones, and then adding a custom image to duplicate nodes.

For this I need loops 4 times. Is there an easier way to do this? I worry about this performance for a large amount of data.

//Append duplicate item nodes with custom icon
function addRemoveForDuplicateItems() {
    var treeView = $('#MyTree').data('t-TreeView li.t-item');
    var myNodes = $("span.my-node", treeView);
    var myNames = [];

    $(myNodes).each(function () {
        myNames.push($(this).text());
    });

    var duplicateItems = getDuplicateItems(myNames);

   $(myNodes).each(function () {
        if (duplicateItems.indexOf($(this).text()) > -1) {
            $(this).parent().append(("<span class='remove'></span>"));
        }
    });
}

//Get all duplicate items removing unique ones
//Input [1,2,3,3,2,2,4,5,6,7,7,7,7] output [2,3,3,2,2,7,7,7,7] 
function getDuplicateItems(myNames) {
    var duplicateItems = [], itemOccurance = {};

    for (var i = 0; i < myNames.length; i++) {
        var dept = myNames[i];
        itemOccurance[dept] = itemOccurance[dept] >= 1 ? itemOccurance[dept] + 1 : 1;
    }
    for (var item in itemOccurance) {
        if (itemOccurance[item] > 1)
            duplicateItems.push(item);
    }
    return duplicateItems;
}
+4
source share
4 answers

If I understand correctly, the whole point here is simply to mark duplicates, right? You must do this in two simple passages:

var seen = {};
var SEEN_ONCE = 1;
var SEEN_DUPE = 2;

// First pass, build object
myNodes.each(function () {
    var name = $(this).text();
    var seen = seen[name];
    seen[name] = seen ? SEEN_DUPE : SEEN_ONCE;
});

// Second pass, append node
myNodes.each(function () {
    var name = $(this).text();
    if (seen[name] === SEEN_DUPE) {
        $(this).parent().append("<span class='remove'></span>");
    }
});

, , DOM , . $(myNodes).each(...), , , . , DOM:

var names = [];
var seen = {};
var SEEN_ONCE = 1;
var SEEN_DUPE = 2;

// First pass, build object
myNodes.each(function () {
    var name = $(this).text();
    var seen = seen[name];
    names.push(name);
    seen[name] = seen ? SEEN_DUPE : SEEN_ONCE;
});

// Second pass, append node only for dupes
names.forEach(function(name, index) {
    if (seen[name] === SEEN_DUPE) {
        myNodes.eq(index).parent()
            .append("<span class='remove'></span>");
    }
});
0

, , , , . itemOccurance , .

var i, dept, itemOccurance = {};
for (i = 0; i < myNames.length; i++) {
    dept = myNames[i];
    if (typeof itemOccurance[dept] == undefined) {
        itemOccurance[dept] = true;
    }
}
0

getDuplicateItems() , , ( myNodes myNames) ( myNodes , span) , . , duplicateItems ! 2 getDuplicateItems(). @user2182349 : return, . return Object.keys(itemOccurance).

0

, getDuplicateItems() :

function getDuplicateItems(myNames) {
    var duplicateItems = [], clonedArray = myNames.concat(), i, dept;
    for(i=0;i<clonedArray.length;i+=1){
        dept = clonedArray[i];
        if(clonedArray.indexOf(dept) !== clonedArray.lastIndexOf(dept)){              
           if(duplicateItems.indexOf(dept) === -1){
              duplicateItems.push(dept);
           }
           /* Remove duplicate found by lastIndexOf, since we've already established that it a duplicate */
           clonedArray.splice(clonedArray.lastIndexOf(dept), 1);  
        }
    }
    return duplicateItems;
}
0

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


All Articles