4

I'm getting my array list through jQuery's each function.

  var spnTxt = '';
  var arr = $('#id').find('span').each(function(utval) {
       spnTxt = $(this).text();
  });

This is giving me

["pet", "dog", "london", "class"]
["pet", "cat", "newyork", "weight"]
["tech", "phone", "spain", "hello2"]
["tech", "phone", "spain", "hello"]
["tech", "phone", "spain", "hello"]

In my above example, i should get

["pet", "dog", "london", "class"]
["pet", "cat", "newyork", "weight"]
["tech", "phone", "spain", "hello2"]
["tech", "phone", "spain", "hello"]

Which is of unique. And my below code doesnt work. I'm not sure if its correct.

var dup = {};
var arr = $('#id').find('span').each(function(utval) {
    spnTxt = $(this).text();
    if (dup[spnTxt])
        $(this).remove();
    else
        dup[spnTxt] = true;

});

Basically i want to remove duplicate array, if my strings in arrays are exactly similar to each other. How to achieve this

3 Answers3

0

To compare arrays to see if their entire collection is the same you can use the Jquery is.

$(array4).is(array5)

This will return true if the arrays are the same. I don't quite see how you're storing the arrays but hopefully this will give you enough information to implement it yourself.

George
  • 6,630
  • 2
  • 29
  • 36
0

Here is the function that will do the trick for you

function arraysEqual(arr1, arr2) {
    if(arr1.length !== arr2.length)
        return false;
    for(var i = arr1.length; i--;) {
        if(arr1[i] !== arr2[i])
            return false;
    }

    return true;
}

Credit goes to this SO LINK

Create a parent Array var parentArray =array();

After each() function returns you an array, just push that array into the parent array

parentArray.push(Your_array_from_the_each_loop);

So after every loop is run using the above function compare it with the elements of the parentArray

You can loop through the parentArray using $.each() function of jQuery

$.each(parentArray,function(key,value){ //NOTE: $.each() is different that $('elem').each();
//do the comparison
});
Community
  • 1
  • 1
Abhinav
  • 8,028
  • 12
  • 48
  • 89
0

try

var newArr = [];

for (var i = 0; i < arr.length; i++) {
    if (!isArraySame(newArr, arr[i])) {
        newArr.push(arr[i]);
    }
}

console.log(newArr)

function isArraySame(ar, check) {

    var isIn = false;
    for (var j = 0; j < ar.length; j++) {
        var sliceArr = ar[j];
        if (ar[j].length == check.length) {
            var count = 0;
            for (var i = 0; i < check.length; i++) {
                if (check[i] == ar[j][i]) {
                    count++;
                }
            }
            if (count == check.length) {
                isIn = true;
                break;
            }
        }
    }
    return isIn;
}

DEMO

or use this method for checking same array

function isArraySame(ar, check) {
    var strArr = check.join();
    var isIn = false;
    for (var j = 0; j < ar.length; j++) {
        if (ar[j].join() == strArr){
            isIn = true;
            break;
        }
    }
    return isIn;
}

arrayString DEMO

Balachandran
  • 9,567
  • 1
  • 16
  • 26