2

So, I'm trying to figure out multi-dimensional arrays.

If I have this multi-dimensional array

var passcodes = [
    [1, 4, 4, 1],
    [1, 2, 3, 1],
    [2, 6, 0, 8],
    [5, 5, 5, 5],
    [4, 3, 4, 3]
];

How do I print out a specific array from within it? In this case, the one with no odd numbers. So far I have this

for (var i = 0; i < passcodes.length; i++) {
  console.log(passcodes[i]);
}

I know that will print out the whole array one at a time but I can't figure out where to go from here.

3
  • 1
    Where do you want to go from there? Does console.log(passcode[1]) help? Commented Oct 2, 2017 at 14:28
  • do you need only the first array which match or all? Commented Oct 2, 2017 at 14:37
  • Are you just trying to understand how multidimensional-arrays work or just trying to find how to return the array(s) with no odd values? One will give you lifetime benefit, the other will cure the itch but not the rash per se. Commented Oct 2, 2017 at 15:02

5 Answers 5

3

Look at this approach. Use Array#filter to iterate over outer array an using Array#every get that ones which has only odd numbers. This will give you the arrays which are have odd numbers. Then using simple for loop iterate over them accessing element using their indexes.

const passcodes = [
    [1, 4, 4, 1],
    [1, 2, 3, 1],
    [2, 6, 0, 8],
    [5, 5, 5, 5],
    [4, 3, 4, 3]
];

const evenArrays = passcodes.filter(item => item.every(number => number % 2 === 0));

const length = evenArrays.length;

for(let i = 0; i < length; i++) {
  
  let innerLength = evenArrays[i].length;

  for(let j = 0; j < innerLength; j++) {
     console.log(evenArrays[i][j]);
  }
  
}

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

2 Comments

The OP is looking for an array with no odd numbers
@YosvelQuintero Thanks.
2

Reference the array that you want to process in the for declaration to iterate on the 3rd array and use the second dimension as you want to access to elements of that.

You could so write :

for (var i = 0; i < passcodes[3].length; i++) {
  console.log(passcodes[3][i]);
}

Comments

2

You could find the array.

var passcodes = [[1, 4, 4, 1], [1, 2, 3, 1], [2, 6, 0, 8], [5, 5, 5, 5], [4, 3, 4, 3]],
    result = passcodes.find(a => a.every(b => !(b % 2)));
    
console.log(result);

3 Comments

That works perfectly. Is result a built-in process (like console.log, var, return, etc...) because I've never heard of it before.
result is just a varaible name.
Oh woops, that makes sense. Still pretty new at js in general.
0

One of the cleaner ways to accomplish this is with a filter, keeping in mind that filterPasscodes will iterate over the passcodes array and the filterFunction must then iterate over the individual arrays that make up the passcodes array:

var passcodes = [
  [1, 4, 4, 1],
  [1, 2, 3, 1],
  [2, 6, 0, 8],
  [5, 5, 5, 5],
  [4, 3, 4, 3]
];

// iterate over each array in passcodes, sending each to the filterFunction
// and returning only those arrays that evaluate to true in the filterFunction
function filterPasscodes(filterFunction) {
  return passcodes.filter(filterFunction);
}

If you only wanted to find one specific array, use the find method:

function filterPasscodes(filterFunction) {
  return passcodes.find(filterFunction);
}

An example filter function for only the passcodes with even numbers:

// given an array, iterate over each element in that array
// and return only those arrays in which each element is even
function allEven(arr) {
  return arr.every(function(element) {
    return element === 0 || element % 2 === 0);
  });
}

Comments

0

var passcodes = [
    [1, 4, 4, 1],
    [1, 2, 3, 1],
    [2, 6, 0, 8],
    [5, 5, 5, 5],
    [4, 3, 4, 3]
];

//needs to match every item
var some = passcodes.map((e) =>  ( e.some((element, index, array) => element == 2  ) ) ? e : false ).filter(u => u);


//needs to match at least one item
var every = passcodes.map((e) =>  ( e.every((element, index, array) => element % 2  ) ) ? e : false ).filter(u => u);


console.log(some);
console.log(every);

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.