noon Array question

Hello all, quick question, lets say I have an array and I would like to get the modulo of that string, what would be the proper syntax to do something like that? can you call a % on a variable/array? something like var myArray = [5,16, 22, 26, 30]; for(i = 0; i < myArray.length; i++){ return myArray[i]; } modNum = myArray[i] % 5

I would like to get a new array with just the modulo of the myArray numbers. Any help would be appreciated, thanks!

Posted Sep 15, 2016, 4:42 PM UTC

04
senocular•

Like this?

var myArray = [5,16, 22, 26, 30];
var myRemainderArray = myArray.map(function (value) {
  return value % 5;
});
console.log(myRemainderArray); //-> [0, 1, 2, 1, 0]
2
orebotOP•

var myArray = [5,16, 22, 26, 30]; var myRemainderArray = myArray.map(function (value) { return value % 5; }); console.log(myRemainderArray); //-> [0, 1, 2, 1, 0]

yes! exactly that. never seen .map will have to check that out

1
senocular•

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

Look at some of the other array methods that do similar things too, like filter and foreach - each taking a function that runs for each element of the array.

1
orebotOP•

really appreciate the help and links! thanks senocular.

1