Align two multidimensional arrays in javascript

I have two multidimensional arrays 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: /

+4
source share
6 answers

As I said in a comment, in a for loop, use <not <= (an array of length N has indices 0 ... N-1) ... and replaces the outer loop with the inner loop and only presses with the value "N" before the end of the outer loop if the inner loop is not pressed with the value "A"

 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> 
+1
source

I believe that the code will not work as it is written. At least this will not give you the expected result. You iterate through a vulc array inside a loop that iterates through reports. And you are pushing to nuovarray inside the inner loop. So I would expect 6 elements in nuovarray, not the 3 elements you expect. Did you try to run it? This is the easiest way to prove infidelity.

0
source
 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); 
0
source

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

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

The some() method checks to see if any element in the array passed the test implemented by the provided function.
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; })); 
0
source

The code displays the given vulc in nuovarray and adds the desired flag to it. The flag is selected by searching reports , and if it is found, 'N' is applied, otherwise 'A' is applied.

 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> 
0
source

If performance matters, you should use something better than 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]); }); 
0
source

All Articles