Replacing arrays within arrays? Help!


#1

Hello. I’ve created an interesting sort of array. I suppose it’s a bunch of arrays within an array.
It looks like this:

        var wallLocations = [[0, 0, 48, 600],
                             [0, 528, 3186, 72],
                             [3144, -100, 42, 700],
                             [93, 384, 225, 30],
                             [891, 159, 342, 69],
                             [1215, 375, 342, 69],
                             [1470, 66, 342, 69],
                             [1800, 342, 342, 69],
                             [2106, 69, 342, 69],
                             [1215, 375, 342, 69],
                             [2301, 465, 63, 63],
                             [2736, 465, 63, 63]];

Now whenever I go to most sites they say to splice the array. But I really don’t understand how I would do that.
My goal is to replace [93, 384, 225, 30] with [0, 525, 372, 72], but I don’t quite understand how that works. If someone could explain that to me, it’d be much appreciated.


#2

One useful tool is Array.prototype.indexOf.

var found = wallLocations.indexOf([93, 384, 225, 30]);
if (found !== -1) {
    wallLocations[found] =  [0, 525, 372, 72];
}

found
-1
wallLocations[3]
Array [ 93, 384, 225, 30 ]
wallLocations[3].indexOf(93)
0

Looks like indexOf cannot find an array. Back to the drawing board.


Another fail…

function listReplace(parent, target, replacement) { 
var truth = false; 
for (var row = 0; row < parent.length; row++) {
for (var col = 0; col < row.length; col++) {
truth = parent[row][col] == target[col];
}
if (! truth) continue;
for (col = 0; col < row.length; col++) {
parent[row][col] = replacement[col];
return true;
}
}
return false;
}


undefined
listReplace(wallLocations, [93, 384, 225, 30], [0, 525, 372, 72])
false

Of course this is seat of the pants. command line code. Need to open a sandbox…


Success…

function listReplace(parent, target, replacement) { 
var truth = false; 
for (var row = 0; row < parent.length; row++) {
for (var col = 0; col < parent[row].length; col++) {
truth = parent[row][col] == target[col];
if (! truth) break;
}
if (! truth) continue;
for (col = 0; col < parent[row].length; col++) {
parent[row][col] = replacement[col];
}
return true;
}
return false;
}

undefined
var wallLocations = [
  [0, 0, 48, 600],
  [0, 528, 3186, 72],
  [3144, -100, 42, 700],
  [93, 384, 225, 30],
  [891, 159, 342, 69],
  [1215, 375, 342, 69],
  [1470, 66, 342, 69],
  [1800, 342, 342, 69],
  [2106, 69, 342, 69],
  [1215, 375, 342, 69],
  [2301, 465, 63, 63],
  [2736, 465, 63, 63]
];
undefined
listReplace(wallLocations, [93, 384, 225, 30], [0, 525, 372, 72])
true
wallLocations[3]
Array [ 0, 525, 372, 72 ]

https://repl.it/KV0U/1

Notice that the inner loop short-circuits on false and the outer loop short circuits on true? The inner is akin to AND logic, and the outer is akin to OR logic. I say ‘akin’ because it is observalbe here merely by coincidence. They bear a similarity, is all.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.