Repeating pattern in Javascript
-
26-12-2019 - |
Question
How would I repeat the transform pattern below?
BoxesFx.prototype._setTransforms = function() {
this.transforms = {
'effect-1' : {
'next' : [
'translate3d(0, ' + (win.height/2+10) + 'px, 0)',
'translate3d(-' + (win.width/2+10) + 'px, 0, 0)',
'translate3d(' + (win.width/2+10) + 'px, 0, 0)',
'translate3d(0, -' + (win.height/2+10) + 'px, 0)',
],
'prev' : [
'translate3d(' + (win.width/2+10) + 'px, 0, 0)',
'translate3d(0, ' + (win.height/2+10) + 'px, 0)',
'translate3d(0, -' + (win.height/2+10) + 'px, 0)',
'translate3d(-' + (win.width/2+10) + 'px, 0, 0)'
]
}}}
Basically, I am trying to have :
'translate3d(0, ' + (win.height/2+10) + 'px, 0)',
'translate3d(-' + (win.width/2+10) + 'px, 0, 0)',
'translate3d(' + (win.width/2+10) + 'px, 0, 0)',
'translate3d(0, -' + (win.height/2+10) + 'px, 0)'
repeat n amount of times, we can say n will be 3, without copying and pasting it in 3 times. Is there a way to duplicate an array? I feel like I may be confusing.. hope I explained it well
Solution
Just define the array outside
var translate =[
'translate3d(0, ' + (win.height/2+10) + 'px, 0)',
'translate3d(-' + (win.width/2+10) + 'px, 0, 0)',
'translate3d(' + (win.width/2+10) + 'px, 0, 0)',
'translate3d(0, -' + (win.height/2+10) + 'px, 0)',
];
BoxesFx.prototype._setTransforms = function() {
this.transforms = {
'effect-1' : {
'next' : translate,
'prev' : translate
}}}
The two arrays you have aren't the same, but I'm assuming they could be, as you can't reference an array twice and get two completely different arrays ?
If you need an actual new copy of the array each time, you can do
'next' : translate.slice(0)
instead
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow