Notifications
Clear all

[Closed] Offset array numbers

Hey guys,

I have an array with some objects in it:

I’d like to offset the numbers automatically (without having to rename them manually), for example:

myArray[1]
myArray[2]
myArray[3]

to

myArray[4]
myArray[5]
myArray[6]

Thanks
Nahuel

3 Replies
 JHN

I think insertItem is the way to go. Just add 3 entries on myArray on position one to shift the original values.

-Johan

What about this?


 fn shiftItems arr shift item: =
 (
 	local shiftArr = #()
 	case item of 
 	(
 		#number: shiftArr = #{1..shift} as array
 	    --add whatever you would like the function to insert here
 		default: shiftArr[shift] = undefined
 	)
 	(shiftArr + arr) --returns new array
 )
 

Usage:
newArray = shiftItems oldArray 3
or
newArray = shiftItems oldArray 3 item:#number –in case you want to insert numbers (or something else, you get the point)

Thanks for your answers!

Both methods worked great!