# RMMZ[Javascript] Shifting only select elements of array?

#### ct_bolt

##### Creator
Another one for ya guys... kind of has me stumped...

Shifting only select elements of array while keeping the rest of the array as is though.

Example:
Code:
``const a = ["A", 4, "B", "C", "D", 1, 2, 3];``
How would I shift only the numbers while keeping the rest of the array as is.

So the first shift should end up like this:
Code:
``["A", 3, "B", "C", "D", 4, 1, 2]``
Second shift like this:
Code:
``["A", 2, "B", "C", "D", 3, 4, 1]``

Thanks again for any/all help advice.

#### Nolonar

##### Veteran
You can do this:
Code:
``a = a.map(element => typeof element === "number" ? shift(element) : element);``

This will give you a new array where only numbers have been shifted.
As for the shift method, you probably want this:
Code:
``const shift = n => n - 1 || 4;``
Obviously, n has to be greater than 0, otherwise you'll get negative numbers.

For more control you can also define shift this way:
Code:
``const shift = (n, max) => n - 1 || max;``

#### Masked

##### Assistant
I think you'll need to give a bit more detail on the logic of what you're trying to achieve with this one, from what you've said the only thing it can think of is swapping the elements on the array, like this:

JavaScript:
``````const a = [1, "a", 3, "b", "c", 4];

let aux = a[2];
a[2] = a[5];
a[5] = aux;``````

Aside from that, is there any particular reason to have strings and numbers mixed on the same array? It's usually not very good to have elements that behave differently on a collection. If you can split this into two arrays, one for strings and another for integers, then it's a lot easier to do (just `pop` and `unshift`).

### Latest Profile Posts

It seems that I have to change the order with which I make the fantasy games on my bucket list, because the intended title is used as a story element in the first two games in the line, and somebody else might take the title for their own game because the term uses real words. I can't risk going over the apparent three year limit for registering trademark ahead of time.
Iorweth
I am interested in collaborations so if you like my art, hit up my messages!

Making good progress with the second chapter.

Still work in progress.

(Real life, it is chronically the fourth chapter that I am making.)
"The Great Wall is great. But, most people can only be the slaves who built it, leaving their bones underneath that wall. They spent their whole life to build that wall and the empire, an empire that enslaves them." I'm just going to turn it into a modern-day sad tale.

### Forum statistics

Threads
110,676
Messages
1,055,104
Members
143,796
Latest member
ShadyBrain_Entertainment