Modern query method for multidimensional js [array of arrays] other than nested for loops

Viewed 110

I have an array [arr] within an array of arrays [ [arr], [arr1], [arr2] ]

e.g.["prague",50,14] in the following arrays array that I want to check for:

[
 ["prague",50,14],
 ["vienna",48.2, 16.4],
 ["bratislava",48.15, 17.1]
]

and ideally I want to query this [arrays] array using a single parameter ["prague",50,14] to find out if my item already exists before deciding whether to add a new item or not to avoid duplicates:

  1. check existing [arrays] array to ensure [arr] does not exist
  2. if [arr] not found, add/push new item into [arrays] array

​

current nested for method

Now the way I currently do this is by iterating the arrays array and in turn looping though each element

for (var i = 0; i < arr.length; i++) {
  for (var k = 0; k < arr[i].length; k++) {
    if (arr[i][0] == "prague" && arr[i][1] == 50 && arr[i][2] == 14){
      console.log(arr[i][0]+", "+arr[i][1]+", "+arr[i][2]); // if item exists already
    } else {
      // code to add new item 
    }
  }
}

​which in itself is not ideal either as if (arr[i][0] == "prague" && arr[i][1] == 50 && arr[i][2] == 14) produces multiple results (see below code snippet)

​

looking for a modern approach

Before I get flooded with "duplicate" post responses I just want to say that I have looked around extensively and read tonnes of posts here on SO etc about arrays, multidimensional arrays, many which were actually objects, and so on but I'm still none the wiser.

I haven't really found a good question/answer using a simple and similar array example so hence I've decided to create this additional post about querying multidimensional js arrays (not object arrays) in the hope they will aid code dabblers like myself

I'm convinced there should be a far simpler modern way to do this which is mainstream acceptable and far easier then my traditional solution.

​

tried to no avail

At first I thought I could do this using Javascript include() method e.g. arr.includes([p1,p2,p3]) or jQuery inArray() method e.g. $.inArray([p1,p2,p3], arr) but neither seems to work.

I suspect it's because these methods don't work with multidimensional arrays like the one I'm dealing with here, right?

Perhaps I should look at a combination of the above coupled with my "traditional" method, perhaps it may improve code readability or perhaps there really is no better way. In any case I'd be grateful to see how peeps on here handle these type of multidimensional array queries.

​

code snippet

// our array of arrays
var arr = [];
arr.push(["prague",50,14]);
arr.push(["vienna",48.2, 16.4]);
arr.push(["bratislava",48.15, 17.1]);


// params to check for
var p1 = "prague", p2 = 50, p3 = 14;
console.log("Check for Params: "+p1+", "+p2+", "+p3)
console.log("Expected values: as above ||\"true\" || \"int\" other than -1");
console.log("Our array of arrays:\n", arr);

console.log("_________________________________________________________");

console.log("Current nested for loop method:")
for (var i = 0; i < arr.length; i++) {
  for (var k = 0; k < arr[i].length; k++) {
    if (arr[i][0] == "prague" && arr[i][1] == 50 && arr[i][2] == 14){
      console.log(arr[i][0]+", "+arr[i][1]+", "+arr[i][2]); // if item exists already
    } else {
      // code to add new item 
    }
  }
}

console.log("_________________________________________________________");

console.log("[Failed using: jQuery inArray() method");
var prague = $.inArray([p1,p2,p3], arr);
console.log(prague);

console.log("_________________________________________________________");

console.log("[Failed using: Javascript includes() method");
var prague2 = arr.includes([p1,p2,p3]);
console.log(prague2);

var prague3 = arr.includes(p1,p2,p3);
console.log(prague3);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

1 Answers

You could stringify the requested array and the items of the given data.

Then find the item.

var data = [["prague", 50, 14], ["vienna", 48.2, 16.4], ["bratislava", 48.15, 17.1]],
    request = ["prague", 50, 14],
    json = JSON.stringify(request),
    item = data.find(a => JSON.stringify(a) === json);

console.log(item);

Related