English 中文(简体)
对比javascript的多层分数
原标题:Compare multi dimentional arrays in javascript

I would like to compare 2 "multidimensional" arrays (arrays nested in arrays).

var old_dataArray=new Array(("id-2", "message", "user"), ("id-1", "message", "user"), ("id-0", "message", "user"));
var new_dataArray=new Array(("id-3", "message", "user"), ("id-2", "message", "user"), ("id-1", "message", "user"));

在此情况下,我要得到“旧数据阿雷”而不是“新数据阿雷”中包含的阵列(“id-3”、“message”、“user”)。

I ve tried with the array_diff function presented here. http://phpjs.org/functions/array_diff:309 But it doesn t really work !.

问题回答

根据我的理解,你想从新数据中获取所有阵列,这些阵列不是旧数据,假定如果每个要素(id-n要素)中的第一个要素相同,那么其他阵列也是如此。 你可以这样做:

// create an array to store our results:
var results = new Array();

// loop through new_dataArray:
outerloop:
for (var i = 0; i < new_dataArray.length; ++i) {

    // loop through old_dataArray to compare the i th element
    // in new_dataArray with each in old_dataArray:
    for (var j = 0; j < old_dataArray.length; ++j) {

        // check if the ids are the same
        if (new_dataArray[i][0] == old_dataArray[j][0])
            // yes it s there, so move on to the next element of new_dataArray
            continue outerloop;

    }

    // if we get here, continue outerloop; was never called so 
    // this element is not in old_dataArray
    results.push(new_dataArray[i]);

}

// now results contains all arrays that are in new_dataArray 
// but not in old_dataArray

EDIT:然而,如果你想要每个阵列的所有要素都平等,而不仅仅是第一个(前)要素,则使用:

// create an array to store our results:
var results = new Array();

// loop through new_dataArray:
outerloop:
for (var i = 0; i < new_dataArray.length; ++i) {

    // loop through old_dataArray to compare the i th element
    // in new_dataArray with each in old_dataArray:
    innerloop:
    for (var j = 0; j < old_dataArray.length; ++j) {

        // check if the arrays are the same size:
        if (new_dataArray[i].length != old_dataArray[j].length)
            // no, so they must be different
            continue innerloop;

        // check if the arrays have the same values
        for (var k = 0; k < old_dataArray[j].length; ++k) {

            if (new_dataArray[i][k] != old_dataArray[j][k])
                // the k th element is different
                continue innerloop;
        }

        // if we get here, then we have found a match, so move on
        continue outerloop;

    }

    // if we get here, continue outerloop; was never called so 
    // this element is not in old_dataArray
    results.push(new_dataArray[i]);

}

// now results contains all arrays that are in new_dataArray 
// but not in old_dataArray
var old_dataArray = [["id-2", "message", "user"], ["id-1", "message", "user"], ["id-0", "message", "user"]];
var new_dataArray = [["id-3", "message", "user"], ["id-2", "message", "user"], ["id-1", "message", "user"]];

var result = [];

for (var i = 0; i < new_dataArray.length ; i++) {
    var isInOldArray = false;
    console.log(new_dataArray[i][0]);

    for (var j = 0; j < old_dataArray.length; j++) {
         if (old_dataArray[j][0] === new_dataArray[i][0]) {
             isInOldArray = true;
             break;
         };
    };

    if (!isInOldArray) {
        result.push(new_dataArray[i]);
    };
};

alert(result);




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签