在Javascript中匹配两个多维数组

时间:2021-05-11 21:38:36

I have two multidimensional array and i want to create a third multidimensional array:

我有两个多维数组,我想创建第三个多维数组:

var reports = [
    [48.98,153.48],
    [12.3,-61.64]
    ];

var vulc = [
    ["ciccio",48.98,153.48],
    ["cicci",12.3,-61.64],
    ["intruso",59.9,99.9]
    ];

And i want to create a new multidimensional array

我想创建一个新的多维数组

var nuovarray= [];  


for (i=0; i<= reports.length; i++) {

   var attivi= reports[i];
   var attlat= attivi[0];
   var attlng= attivi[1];

    for (s=0;  s<=vulc.length; s++){
     var vulca= vulc[s];
     var vulcanam= vulca[0];
     var vulcalat= vulca[1];
     var vulcalng= vulca[2];

        if ((vulcalat==attlat) && (vulcalng==attlng){
            var stato= "A";
            nuovarray.push([vulcanam,vulcalat,vulcalng,stato]);     
        } 
        else{
            var stato= "N";
            nuovaarray.push([vulcanam,vulcalat,vulcalng,stato]);     
        }    

    }

}

i would like to have

我想拥有

var nuovarray= [
    ["ciccio",48.98,153.48,"N"],
    ["cicci",12.3,-61.64,"N"],
    ["intruso",59.9,99.9,"A"]
    ];

But i don't know if this code is good :/

但我不知道这段代码是否合适:/

6 个解决方案

#1


1  

As I said in the comment, in the for loop, use < not <= (array of length N has indexes 0 ... N-1) ... and swap the outer loop with the inner loop, and only push with value 'N' before the end of the outer loop if the inner loop hasn't pushed with value 'A'

正如我在评论中所说,在for循环中,使用

var reports = [
    [48.98,153.48],
    [12.3,-61.64]
];

var vulc = [
    ["ciccio",48.98,153.48],
    ["cicci",12.3,-61.64],
    ["intruso",59.9,99.9]
];

var nuovarray= [];  

for(var s = 0; s < vulc.length; s++) {
    var vulca = vulc[s];
    var stato= "A"; // default, no match
    var vulcanam= vulca[0];
    var vulcalat= vulca[1];
    var vulcalng= vulca[2];

    for(var i = 0; i < reports.length; i++) {
        var attivi = reports[i];
        var attlat= attivi[0];
        var attlng= attivi[1];
    
        if ((vulcalat==attlat) && (vulcalng==attlng)) {
            stato = "N";
            break; // we've found a match, so set stato = N and stop looping
        }
    }
    nuovarray.push([vulcanam,vulcalat,vulcalng,stato]);     
}


document.getElementById('result').innerHTML = (nuovarray).toSource();
<div id='result'></div>

#2


0  

I believe the code will not work the way it is written. At least, it will not give you the expected output. You are iterating through the vulc array inside the loop which iterates through reports. And you are pushing to the nuovarray inside the inner loop. So I would expect 6 elements in nuovarray, not the 3 elements you are expecting. Did you try running it? That's the easiest way to prove incorrectness.

我相信代码不会按照它的编写方式工作。至少,它不会给你预期的输出。您正在循环遍历循环中的vulc数组,该数组遍历报告。而你正在推动内循环内的nuovarray。所以我期待nuovarray中的6个元素,而不是你期望的3个元素。你试过试试吗?这是证明不正确的最简单方法。

#3


0  

var reports = [
    [48.98,153.48],
    [12.3,-61.64]
];

var vulc = [
    ["ciccio",48.98,153.48],
    ["cicci",12.3,-61.64],
    ["intruso",59.9,99.9]
];

var nuovarray = [];

vulc.forEach(function(item, indx){

    var bN = 'undefined' !== typeof reports[indx];
    bN = bN && item[1] == reports[indx][0] && item[2] == reports[indx][1];

    item.push(bN ? 'N' : 'A'); 

    nuovarray.push(item);

});

console.log(nuovarray);

#4


0  

The code maps the given vulc to nuovarray and add the wanted flag to it. The flag is selected by a search over reports and if found, an 'N' is applied, otherwise an 'A' is applied.

代码将给定的vulc映射到nuovarray并向其添加所需的标志。通过搜索报告来选择该标志,如果找到,则应用“N”,否则应用“A”。

var reports = [
      [48.98, 153.48],
      [12.3, -61.64]
    ],
    vulc = [
      ["ciccio", 48.98, 153.48],
      ["cicci", 12.3, -61.64],
      ["intruso", 59.9, 99.9]
    ],
    nuovarray = vulc.map(function (a) {
        a.push(reports.some(function (b) {
            return a[1] === b[0] && a[2] === b[1];
        }) ? 'N' : 'A')
        return a;
    });
document.getElementById('out').innerHTML = JSON.stringify(nuovarray, null, 4);
<pre id="out"></pre>

#5


0  

The map() method creates a new array with the results of calling a provided function on every element in this array.
Array.prototype.map()

The push() method adds one or more elements to the end of an array and returns the new length of the array.
Array.prototype.push()

The some() method tests whether some element in the array passes the test implemented by the provided function.
Array.prototype.some()

map()方法创建一个新数组,其结果是在此数组中的每个元素上调用提供的函数。 Array.prototype.map()push()方法将一个或多个元素添加到数组的末尾,并返回数组的新长度。 Array.prototype.push()some()方法测试数组中的某个元素是否通过了由提供的函数实现的测试。 Array.prototype.some()

var reports = [
  [48.98,153.48],
  [12.3,-61.64]
];

var vulc = [
  ["ciccio",48.98,153.48],
  ["cicci",12.3,-61.64],
  ["intruso",59.9,99.9]
];

console.log(vulc.map(function (item, index) {
  item.push(reports.some(function (report) {
    return report[0] == item[1] && report[1] == item[2];
  })?"N":"A");

  return item;
}));

#6


0  

If performance matters, you should use something better than O(n^2):

如果性能很重要,你应该使用比O(n ^ 2)更好的东西:

var existingPoints = {};
reports.forEach(function (row) {
    existingPoints[row.join()] = true;
});

var nuovarray = vulc.map(function (row) {
    var point = row.slice(1, 3).join();
    var flag = existingPoints[point] ? 'A' : 'N';
    return row.concat([flag]);
});

#1


1  

As I said in the comment, in the for loop, use < not <= (array of length N has indexes 0 ... N-1) ... and swap the outer loop with the inner loop, and only push with value 'N' before the end of the outer loop if the inner loop hasn't pushed with value 'A'

正如我在评论中所说,在for循环中,使用

var reports = [
    [48.98,153.48],
    [12.3,-61.64]
];

var vulc = [
    ["ciccio",48.98,153.48],
    ["cicci",12.3,-61.64],
    ["intruso",59.9,99.9]
];

var nuovarray= [];  

for(var s = 0; s < vulc.length; s++) {
    var vulca = vulc[s];
    var stato= "A"; // default, no match
    var vulcanam= vulca[0];
    var vulcalat= vulca[1];
    var vulcalng= vulca[2];

    for(var i = 0; i < reports.length; i++) {
        var attivi = reports[i];
        var attlat= attivi[0];
        var attlng= attivi[1];
    
        if ((vulcalat==attlat) && (vulcalng==attlng)) {
            stato = "N";
            break; // we've found a match, so set stato = N and stop looping
        }
    }
    nuovarray.push([vulcanam,vulcalat,vulcalng,stato]);     
}


document.getElementById('result').innerHTML = (nuovarray).toSource();
<div id='result'></div>

#2


0  

I believe the code will not work the way it is written. At least, it will not give you the expected output. You are iterating through the vulc array inside the loop which iterates through reports. And you are pushing to the nuovarray inside the inner loop. So I would expect 6 elements in nuovarray, not the 3 elements you are expecting. Did you try running it? That's the easiest way to prove incorrectness.

我相信代码不会按照它的编写方式工作。至少,它不会给你预期的输出。您正在循环遍历循环中的vulc数组,该数组遍历报告。而你正在推动内循环内的nuovarray。所以我期待nuovarray中的6个元素,而不是你期望的3个元素。你试过试试吗?这是证明不正确的最简单方法。

#3


0  

var reports = [
    [48.98,153.48],
    [12.3,-61.64]
];

var vulc = [
    ["ciccio",48.98,153.48],
    ["cicci",12.3,-61.64],
    ["intruso",59.9,99.9]
];

var nuovarray = [];

vulc.forEach(function(item, indx){

    var bN = 'undefined' !== typeof reports[indx];
    bN = bN && item[1] == reports[indx][0] && item[2] == reports[indx][1];

    item.push(bN ? 'N' : 'A'); 

    nuovarray.push(item);

});

console.log(nuovarray);

#4


0  

The code maps the given vulc to nuovarray and add the wanted flag to it. The flag is selected by a search over reports and if found, an 'N' is applied, otherwise an 'A' is applied.

代码将给定的vulc映射到nuovarray并向其添加所需的标志。通过搜索报告来选择该标志,如果找到,则应用“N”,否则应用“A”。

var reports = [
      [48.98, 153.48],
      [12.3, -61.64]
    ],
    vulc = [
      ["ciccio", 48.98, 153.48],
      ["cicci", 12.3, -61.64],
      ["intruso", 59.9, 99.9]
    ],
    nuovarray = vulc.map(function (a) {
        a.push(reports.some(function (b) {
            return a[1] === b[0] && a[2] === b[1];
        }) ? 'N' : 'A')
        return a;
    });
document.getElementById('out').innerHTML = JSON.stringify(nuovarray, null, 4);
<pre id="out"></pre>

#5


0  

The map() method creates a new array with the results of calling a provided function on every element in this array.
Array.prototype.map()

The push() method adds one or more elements to the end of an array and returns the new length of the array.
Array.prototype.push()

The some() method tests whether some element in the array passes the test implemented by the provided function.
Array.prototype.some()

map()方法创建一个新数组,其结果是在此数组中的每个元素上调用提供的函数。 Array.prototype.map()push()方法将一个或多个元素添加到数组的末尾,并返回数组的新长度。 Array.prototype.push()some()方法测试数组中的某个元素是否通过了由提供的函数实现的测试。 Array.prototype.some()

var reports = [
  [48.98,153.48],
  [12.3,-61.64]
];

var vulc = [
  ["ciccio",48.98,153.48],
  ["cicci",12.3,-61.64],
  ["intruso",59.9,99.9]
];

console.log(vulc.map(function (item, index) {
  item.push(reports.some(function (report) {
    return report[0] == item[1] && report[1] == item[2];
  })?"N":"A");

  return item;
}));

#6


0  

If performance matters, you should use something better than O(n^2):

如果性能很重要,你应该使用比O(n ^ 2)更好的东西:

var existingPoints = {};
reports.forEach(function (row) {
    existingPoints[row.join()] = true;
});

var nuovarray = vulc.map(function (row) {
    var point = row.slice(1, 3).join();
    var flag = existingPoints[point] ? 'A' : 'N';
    return row.concat([flag]);
});