简体   繁体   中英

Find missing element by comparing 2 2D-Arrays in Javascript

A few days ago I posted a thread asking on how to find the missing element when passing a method 2 JS arrays. As you can see here . I've been trying to figure out now how to modify the method so that instead of passing it 2 Arrays you pass it 2 2D-Arrays... Having some trouble though:

/*var sml = new Array();
sml[0] = new Array("dean","22");
sml[1] = new Array("james","31");
sml[2] = new Array("ludwig","35");

var lrg = new Array();
lrg[0] = new Array("dean","22");
lrg[1] = new Array("james","31");
lrg[2] = new Array("ludwig","35");
lrg[3] = new Array("kevin","23");
lrg[4] = new Array("elton","40");*/

var sml = new Array();
sml[0] = "dean";
sml[1] = "james";
sml[2] = "ludwig";

var lrg = new Array();
lrg[0] = "dean";
lrg[1] = "james";
lrg[2] = "ludwig";
lrg[3] = "kevin";
lrg[4] = "elton";


var deselected = findDeselectedItem(sml, lrg);

alert("Deselected Items: " + deselected[0]+", "+ deselected[1]);

// -------------------------------------------------------------- //

function findDeselectedItem(CurrentArray, PreviousArray) {


var CurrentArrSize = CurrentArray.length;
var PreviousArrSize = PreviousArray.length;
var deselectedItems = new Array();

// loop through previous array
for (var j = 0; j < PreviousArrSize; j++) {

    // look for same thing in new array
    if (CurrentArray.indexOf(PreviousArray[j]) == -1)

    deselectedItems.push(PreviousArray[j]);

}

if (deselectedItems.length != 0) {
    return deselectedItems;
} else {
    return null;
}

}​

Now if you run the above code it works perfectly, but if you go and uncomment the variable declarations on top that that pushes arrays ontop of the array, and then comment out the simple strings that get pushed on top of the array, it doesn't work as well... For instance:

var sml = new Array();
sml[0] = new Array("dean","22");
sml[1] = new Array("james","31");
sml[2] = new Array("ludwig","35");

var lrg = new Array();
lrg[0] = new Array("dean","22");
lrg[1] = new Array("james","31");
lrg[2] = new Array("ludwig","35");
lrg[3] = new Array("kevin","23");
lrg[4] = new Array("elton","40");

/*var sml = new Array();
sml[0] = "dean";
sml[1] = "james";
sml[2] = "ludwig";

var lrg = new Array();
lrg[0] = "dean";
lrg[1] = "james";
lrg[2] = "ludwig";
lrg[3] = "kevin";
lrg[4] = "elton";*/


var deselected = findDeselectedItem(sml, lrg);

alert("Deselected Items: " + deselected[0]+", "+ deselected[1]);

// -------------------------------------------------------------- //

function findDeselectedItem(CurrentArray, PreviousArray) {


var CurrentArrSize = CurrentArray.length;
var PreviousArrSize = PreviousArray.length;
var deselectedItems = new Array();

// loop through previous array
for (var j = 0; j < PreviousArrSize; j++) {

    // look for same thing in new array
    if (CurrentArray.indexOf(PreviousArray[j][0]) == -1)

    deselectedItems.push(PreviousArray[j][0]);

}

if (deselectedItems.length != 0) {
    return deselectedItems;
} else {
    return null;
}

}​

The method returns 2 completely wrong values. PS - I'm not interested in the "numbers" just yet, just the "names" for now...

Your CurrentArray is 2-dimensional and indexOf compares Arrays but not first elements of that Arrays. So you need to use:

for ( var i = 0; i < CurrentArray.length; ++i){
    if (CurrentArray[i][0] == PreviousArray[j][0]){
        deselectedItems.push(PreviousArray[j][0]);
        break;
    }
}

Instead of

if (CurrentArray.indexOf(PreviousArray[j][0]) == -1)
    deselectedItems.push(PreviousArray[j][0]);

indexOf function will check object equality in this case; to make it return something else than -1 you've to pass same 2D array instances to sml and lrg.

new Array("dean","22") === new Array("dean","22") //false

Keep the same instances (eg http://jsfiddle.net/3TQYz/ ) in both arrays or use your own indexOf test that would recursively check values of the array to make your case working.

You could also rearrange the array like this:

var sml = {};
sml["dean"] = 22;
sml["james"] = 31;
sml["ludwig"] = 35;

var lrg = {};
lrg["dean"] = 22;
lrg["james"] = 31;
lrg["ludwig"] = 35;
lrg["kevin"] = 23;
lrg["elton"] = 40;

and use:

function findDeselectedItem(c,p){
    ret=[];
    for (var i in p){
        if (p.hasOwnProperty(i)){
            if ('undefined'===typeof c[i]) {
                ret.push(i);
            }
        }
    }
    return ret;
}


alert(findDeselectedItem(sml, lrg));

Demo: http://jsfiddle.net/LsrCj/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM