Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put multiple elements to JavaScript Array

Tags:

javascript

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.

like image 697
Ωmega Avatar asked Dec 03 '22 03:12

Ωmega


2 Answers

var myArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 
               12, 13, 14, 15, 16, 17, 18, 19, 20];
myArray.splice(12, 4, 123, 432, 233, 98);

// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 
//  123, 432, 233, 98, 16, 17, 18, 19, 20]

The first parameter to splice is 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:

var oldArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
               12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
               22, 23, 24, 25, 26, 27, 28, 29, 123, 
               432, 233, 98, 34, 35]
var newArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 
               12, 13, 14, 15, 16, 17, 18, 19, 20];

[].splice.apply(newArray, [12, 4].concat(oldArray.slice(30, 30 + 4)));

// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
// 123, 432, 233, 98, 16, 17, 18, 19, 20]

This is probably less efficient than PointedEars suggestion but it's quite simple and would probably serve for many use cases.

like image 107
Scott Sauyet Avatar answered Dec 21 '22 02:12

Scott Sauyet


The closest you can come is to use splice to set the input position.

The first argument is the index to at which to begin, the second argument is how many items to remove, and the next arguments are the values....

Here is some output I get messing around in a console.

myArray = new Array(15)
myArray.splice(12, 4, 123, 432, 223, 98)
myArray.forEach(function(v, i){console.log(v, i)})
123 12
432 13
223 14
98 15
undefined
myArray[12]
123
myArray[13]
432
myArray[14]
223
myArray[15]
98

Of course this has several deficiencies: you can't pass an array of values you want to insert, the array needs to be large enough (i.e you cant insert at index 12 unless the array is at least 12 long).

For transferring values at one set of indexes in one array to another set of indexes in another array, I think you have to loop. splice takes the values to add individually, not as an array. You could of course write a single method with the loop, and reuse it.

like image 20
hvgotcodes Avatar answered Dec 21 '22 01:12

hvgotcodes