In JavaScript I am filling array elements by traditional way, such as:
myArray[12] = 123;
myArray[13] = 432;
myArray[14] = 233;
myArray[15] = 98;
Is it possible to do this faster by one command?
For example to put numbers [123, 432, 233, 98] to myArray[12-15] … somehow? Not one by one.
And how can be optimized this kind of scenario:
newArray[12] = oldArray[30];
newArray[13] = oldArray[31];
newArray[14] = oldArray[32];
newArray[15] = oldArray[33];
when n elements (4 in the above example) next each to other from one array need to be moved to another array?
Please advice.
The first parameter to
spliceis the index to start with, the second is the number of elements to remove, the remainder are elements to insert. So if you insert the same number as were removed, you end up replacing them.If you do want this to come from another array, you could try this, although it is not as clean:
This is probably less efficient than PointedEars suggestion but it’s quite simple and would probably serve for many use cases.