1

Following code i wanted to share which compares two array of array:-

var x = [["x", "r", "t"], ["a", "b", "n"], ["j", "l", "x"]];
var y = [["y", "w", "z"], ["a", "b", "n"], ["j", "l", "x"]];
var objX = [];
var objY = [];
for (var i = 0; i < x.length; i++)
{
    objX[i] = {};
    for (var j = 0; j < x[i].length; j++)
    {
        objX[i][x[i][j]] = i;
    }

}

for (var i = 0; i < y.length; i++)
{
    objY[i] = {};
    for (var j = 0; j < y[i].length; j++)
    {
        objY[i][y[i][j]] = i;
    }

}
Object.size = function(obj) {
    var size = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key))
            size++;
    }
    return size;
};
function compareObjs(oA, oB)
{
    if (Object.size(oA) > Object.size(oB))
    {
        aa = oA;
        ba = oB;
    }
    else
    {
        aa = oB;
        ba = oA;
    }
    for (var property in aa) {
        if (!ba.hasOwnProperty(property)) {
            return false;
        }
    }
    return true;
}
function compareArrayOfObj(aA, aB)
{
    var aIb = [];
    var aMb = [];
    var bMa = [];
    var aIIndex = [];
    var bIIndex = [];
    var aIF = [];
    var bIF = [];
    if (aA.length > aB.length)
    {
        a = aA;
        b = aB;
    }
    else
    {
        a = aB;
        b = aA;
    }
    for (var i in a)
    {
        for (var j in b)
        {

            if (compareObjs(a[i], b[j]))
            {
                for (var blah in a[i])
                {
                    aIb.push(x[a[i][blah]]);
                    break;
                }
                aIIndex.push(i);
                bIIndex.push(j);

            }
        }
    }
    for (var i in a)
    {
        if (aIIndex.indexOf(i) == -1)
        {
            for (var blah in a[i])
            {
                aIF.push(x[a[i][blah]]);
                break;
            }
        }
    }
    for (var j in b)
    {
        if (bIIndex.indexOf(j) == -1)
        {
            for (var blah in b[j])
            {
                bIF.push(y[b[j][blah]]);
                break;
            }
        }
    }


    return  {"aIb": aIb, "aMb": aIF, "bMa": bIF}
}

resultSet=compareArrayOfObj(objX, objY);




/*
resultSet object holds three properties as:-

1. aIb = objX Intersection objY 
2. aMb = objX - objY 
3. bMa = objY - objX
*/

Improvisation in the code is always welcome. Basically i have written this code to have set operation on arrays. It will return A minus B,B minus A and A intersection B.

8
  • Is there a question here? Commented Sep 6, 2013 at 11:56
  • no question, just wanted to share the code Commented Sep 6, 2013 at 11:57
  • gist.github.com would likely be a better choice since some So members would downvote or close this for being off topic unless it was an answer to a question Commented Sep 6, 2013 at 12:08
  • This question appears to be off-topic because it isn't a question. Commented Sep 6, 2013 at 12:12
  • will post it on gist.github.com also...thax :) Commented Sep 6, 2013 at 12:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.