4

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 :/

7
  • If your code works and you only want to know if/how it's good, then your question is on-topic to CR. Here it's off-topic/ Commented Aug 2, 2015 at 10:11
  • 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' Commented Aug 2, 2015 at 10:21
  • @JaromandaX yes the <= it was a stupid error, but why i must swap loops ? Commented Aug 2, 2015 at 10:35
  • it makes sense to swap loops ... based on your sample output, you want one output per vulc, not one output per reports - so, you process each vulc to see if a report matches, not the other way around Commented Aug 2, 2015 at 10:45
  • I think you need a different approach, do you really need a 3 dimesions array or you just need to keep the state in the 2 dimesions arrary? Commented Aug 2, 2015 at 11:00

6 Answers 6

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'

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>

Sign up to request clarification or add additional context in comments.

5 Comments

the problem is that i would like an example using for loop and not methods that i don't know :/ thanks
i'm sure that your code is good and works, but i don't understand it :((
fair enough - let me rewrite it
you are too kind and patient ! :)
works very good! is a few months that I do not program in Javascript and I had forgotten the use of "break"! thanks a lot!!!
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.

3 Comments

This should be a comment, not answer
yes and doesn't works... is difficult for me match two multidimensional array, so i hope in help :(
i'm sure that your code is good and works, but i don't understand it :((
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);

2 Comments

why .map and not .forEach - your not actually 'map'ping anything?
.map returns a copy of the array. .forEach doesn't
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.

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>

Comments

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()

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;
}));

Comments

0

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]);
});

Comments

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.