0

Lets say I have a Multi-Dimensional array as per the following code:

var theArray = [
  [1, 2, 3],
  [3, 2, 1],
  [1, 2, 3],
  [1, 2, 1]
]

I need to perform an iteration where I mutate the digits based on some conditions:

  • If digit 1 has a 3 below it; it transforms to "foo".
  • If digit 3 has a 1 above it; it transforms to "bar".
  • If a vertical consecutive sequence of 2 pairs of 1 & 3 or 3 & 1 occurs, no mutation occurs to the pairs.

My issue is that when I iterate through it using forEach(), I end up mutating the digits in the current array before i get to the next array, and so I invalidate the conditions because once the iteration goes to the next array; the checks fail because there will be "foo" or "bar" for example instead of 1 or 2.

So something like this isn't going to work:

someArray.forEach((array, index) => {
  array.forEach((digit, numIndex) => {
    if(someArray[index + 1][numIndex] == 3 && digit == 1) {
      array[numIndex] = "foo"
    } else if(someArray[index - 1][numIndex] == 1 && digit == 3) {
      array[numIndex] = "bar"
    }
  })
})

Therefore; I presume there has to be a way to "live check" if these conditions exist through all arrays at the same time. Especially taking into consideration the last constriction which would need to check through vertical pairs.

Expected result:

var theArray = [
  ["foo", 2, 3],
  ["bar" , 2, 1],
  [1, 2, 3]
  [1, 2, 1]
]

How is this achieved ?

Thanks in advance for the help!

5
  • It is not possible to return a value from Array.prototype.forEach() Commented Jul 23, 2017 at 19:09
  • copy the arrays. Iterate ower one and mutate the other if that was the issues Commented Jul 23, 2017 at 19:11
  • @guest271314 thanks for pointing that out, I fixed it up, this was supposed to be semi pseudocode just as an example. Commented Jul 23, 2017 at 19:17
  • What is the expected resulting array? [ [ "foo", 2, 3 ], [ 3, 2, "foo" ], [ 1, 2, 3 ], [ 1, 2, 1 ] ]? Commented Jul 23, 2017 at 19:18
  • Thanks for your response, I've added the expected result @guest271314 Commented Jul 23, 2017 at 19:26

1 Answer 1

1

First check if the element of the array exists at +/- index, then check if all of the elements at that index contain only 1 or 3 using .every()

var theArray = [
  [1, 2, 3],
  [3, 2, 1],
  [1, 2, 3],
  [1, 2, 1]
];

var not = [1,3,1,3];

theArray.forEach((array, index) => {
  array.forEach((digit, numIndex) => {
    if(theArray[index +1] && theArray[index + 1][numIndex] 
      && theArray[index + 1][numIndex]  == 3 && digit == 1 
      && !theArray.map(arr => arr[index +1])
         .every((el, i) => el === not[i]) && !theArray.map(arr => arr[index +1])
         .every((el, i) => el === not.slice(0).reverse()[i])) {
      array[numIndex] = "foo"
    } 
    else if(theArray[index - 1] && theArray[index - 1][numIndex] 
         && theArray[index -1][numIndex] == "foo" && digit == 3) {
      if (!theArray.map(arr => arr[index -1])
         .every((el, i) => el === not[i]) && !theArray.map(arr => arr[index -1])
         .every((el, i) => el === not.slice(0).reverse()[i]))
      array[numIndex] = "bar"
    }
  })
});

console.log(theArray);

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

4 Comments

Thanks for your answer; thats exactly what i wanted, but just for clarity; your using .every() to check that elements at that index are only 1 or 3, ho is this checking for consecutive vertical pairs? @guest271314
I've just tried this in console and unfortunately this doesn't check for consecutive pairs, it checks to see if all the elements at that index are 1 or 3 but I need this to check if they are pairs!!
@hyper0009 Good point. You can define an array containing the elements which theArray should not contain at that specific index, check if the current element is set at theArray both forwards or reversed. See updated post
Thanks for the update, this is precisely what i wanted; I can create different arrays to cross check against the matrix for different sequences aswell with this. @guest271314

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.