Skip to content Skip to sidebar Skip to footer

Compare Two Arrays Based On Length: Skip Empty Values

I have a form with 4 input (can be even many more) where the user can put a number or nothing. The only rule is that if you put a number in a input you cannot submit if the same nu

Solution 1:

Here is another possible way to handle it. Here is the working JSFiddle. And here is the code:

$(function() {
    $("#submit").click(function() {
        //build a profile of the inputs
        var inputs = [];
        var values = [];
        var dups = false; //track duplicates on pass 1

        $(".seq").each(function(i, el) {
            var empty = (el.value == ""); //check if empty
            var exists = (!empty && $.grep(inputs, function(item, index) {
                return (item.Value === el.value);
            }).length > 0); //check if exists
            dups = (dups || exists); //track dups

            //add the new input item
            var obj = {
                Element: el,
                Value: el.value,
                Empty: empty,
                Exists: exists
            };
            inputs.push(obj);

            //conditionally add the sorting value
            if (!empty && !exists)
                values.push(el.value);
        });

        //Validate the inputs. If there are duplicates, don't submit
        $(".seq").css("background-color", "white"); //clear errors
        if (dups) {
            $(inputs).each(function(i, el) {
                if (el.Exists) 
                    el.Element.style.backgroundColor = "red";
            });
        } else {
            values = values.sort();
            alert(values);
        }
    });
});

With this method, at the end you have an array - inputs - of all of the elements with their statuses so that you can provide error handling on specific fields. In my example, the error fields turn red.

At the alert, you have a sorted array of the valid values.


Solution 2:

Use a hash to track values as you iterate. This example simply returns true or false, but you could also scan the entire array and return the repeated values.

function uniquifyArray(ary) { 
    var seen = {};
    var isUnique = true;
    /* iterate backwards since the array length will change as elements are removed */
    for (var i=ary.length; i--;) {
        /* remove blank/undefined */
        if (typeof ary[i] === 'undefined' || ary[i] === '') {                
            ary.splice(i,1);
        } else {   
            /* check if this value has already been seen */
            if (ary[i] in seen) { 
                isUnique = false;
                ary.splice(i,1);
            } else { 
                seen[ary[i]]=true;   
            }
        }
    }
    ary = ary.sort();
    return isUnique;
}

var test = [ '1','2','','','3','4','1' ];
uniquifyArray(test); // returns false, test = [ '1','2','3','4' ]

test = [ '1','2','','' ]
uniquifyArray(test); //true, test = ['1','2']

Post a Comment for "Compare Two Arrays Based On Length: Skip Empty Values"