JavaScript – Continuity of an array using arithmetic operations to find the index

Question:

In JavaScript, I, using in the ordinary for loop case of array iteration with variable i , am trying to create an array with sub-arrays that have data from other arrays from an order relative to the position of i .

This code example describes situation:

var arranjoBase1 = ["a", "b", "c", "d", "e"]
var arranjoBase2 = ["f", "g", "h", "i", "j"]
var arranjoBase3 = [1, 2, 3, 4, 5]

var arranjoFinal = [ [], [], [], [], [] ]

for(i = 0; i < arranjoFinal.length; i++)
 { arranjoFinal[i].push(arranjoBase1[i], arranjoBase2[i], arranjoBase3[i]);
   if(i != 0) { arranjoFinal[i].push(arranjoBase3[i - 1]) }
    else { arranjoFinal[i].push(arranjoBase3[arranjoBase3.length - 1]) };
   if(i != arranjoBase3.length - 1) { arranjoFinal[i].push(arranjoBase3[i + 1]) }
    else { arranjoFinal[i].push(arranjoBase3[0]) };
   if(i == 0) { arranjoFinal[i].push(arranjoBase3[arranjoBase3.length - 2]) } 
    else if(i == 1) { arranjoFinal[i].push(arranjoBase3[arranjoBase3.length - 1 ]) }
    else { arranjoFinal[i].push(arranjoBase3[i - 2]) };
   if(i == arranjoBase3.length - 1) { arranjoFinal[i].push(arranjoBase3[1]) }
    else if(i == arranjoBase3.length - 2) { arranjoFinal[i].push(arranjoBase3[0]) }
    else { arranjoFinal[i].push(arranjoBase3[i + 2]) }  };

//Resultado Final
  var arranjoFinal = (5) [Array(7), Array(7), Array(7), Array(7), Array(7)]

//Exemplo de Arranjo Final
  var  arranjoFinal[0] = (7) ["a", "f", 1, 5, 2, 4, 3]

This code already gives me the desired results.

The point is that, as I had to use many if conditionals to get around the problem of if the result of one of the arithmetic operations to find the next element to add to the FinalArray is outside the index of the Base3Array – in which case it would be returned undefined instead of desired element – ​​I feel that there must be a much more practical solution to achieve this result, which, for example, is also sustainable for arrangements larger than five elements.

So, is there really a more [practical/concise] way to achieve results like these? As, for example, and preferably, a way of saying that, if the result of an arithmetic operation to find the element index of an array exceeds the index of that array, it means that it is for it to continue the operation from its [beginning /Final]?

Answer:

Alright, let's go:

I believe your conditions are the least of your problems.

I didn't try to improve the conditions because I was a little confused, I believe you need to think of better logic.

But the point is to improve javascript practices in this code.

A function that will help your life a lot is map , it returns a new array from its input array (the original array is unmodified, which means a lot in functional programming).

Your comparisons should be === instead of == to avoid type conversion since you know the variables you are comparing are numbers.

Use const or let , var is no longer in use. In my example I only used const because despite having to change the elements of an array, I didn't change its memory address.

Improve your indentation: your conditionals are very difficult to understand, don't put a semicolon at the end of an else. I advise you to use eslint which provides rules for coding and ends up teaching you how to program better.

As a last tip, learn functional programming and see why not mutating an input variable is important for better maintenance and debugging of your code and learn javascript in version 6.

I made a code using arrow functions that is version 6 and it already works in chrome and firefox:

const arranjoBase1 = ["a", "b", "c", "d", "e"]
const arranjoBase2 = ["f", "g", "h", "i", "j"]
const arranjoBase3 = [1, 2, 3, 4, 5]

const arranjoBase = [undefined, undefined, undefined, undefined, undefined]

const arranjoMap = arranjoBase.map((el, i) => {
    const arranjoParte1 = [arranjoBase1[i], arranjoBase2[i], arranjoBase3[i]];
    const arranjoParte2 = [];

  if (i !== 0) {
    arranjoParte2.push(arranjoBase3[i - 1]);
  } else {
    arranjoParte2.push(arranjoBase3[arranjoBase3.length - 1]);
  }

  if(i !== arranjoBase3.length - 1) {
    arranjoParte2.push(arranjoBase3[i + 1]);
  }
  else {
    arranjoParte2.push(arranjoBase3[0]);
  }

  if (i === 0) {
    arranjoParte2.push(arranjoBase3[arranjoBase3.length - 2]);
  } else if (i === 1) {
    arranjoParte2.push(arranjoBase3[arranjoBase3.length - 1 ]);
  } else {
    arranjoParte2.push(arranjoBase3[i - 2]);
  }

  if (i === arranjoBase3.length - 1) {
    arranjoParte2.push(arranjoBase3[1]);
  }
  else if (i === arranjoBase3.length - 2) {
    arranjoParte2.push(arranjoBase3[0]);
  } else {
    arranjoParte2.push(arranjoBase3[i + 2]);
  }

  return arranjoParte1.concat(arranjoParte2);
});

As I'm going to use the map, I just need an array with the number of elements you need. As you only need the values ​​of the other 3 arrays the array value doesn't matter.

ArrangePart1 is always fixed and ArrangePart2 is handled in conditions. At each interaction I return an array with the concatenation of the two.

In version 5 I use concat to concatenate, in version 6 I can just use this command:

[...arranjoParte1, ...arranjoParte2] 

Here 's a working example, because after all it's easy, right 😉

Scroll to Top