I have an array of 5 elements ex. arr = [1,2,3,4,5]
; I want to add elements between these elements, and place them in new array arr1 = [1,0,0,0,2,0,0,0,3,0,0,0,4,0,0,0,5,0,0,0];
. When i alert arr1
i get the same array as it is, but when i alert arr1.length
i get that it`s length is 5, when it is actually 20. Can you help me fix this, or tell me why do i get that result. Here is an example of the code i am using:
function niza(val,times){
var arr = [];
for (var i=0;i<times;i++) {
arr.push(val);
}
return arr;
}
and then this:
var y1=0;
var arr= [];
var a = new Array();
for (var j=0;j<Niza1.length;j++) {
y1 = Niza1[j];
arr = y1 + "," + niza(0,11);
a.push(arr);
}
where Niza1 holds the 5 elements mentioned before in arr
, and a holds the elements mentioned in arr1
.
-
You're creating a new Array that will be given 5 strings. I can't reconcile that with your desired result. Is this some sort of homework assignment?cookie monster– cookie monster2014年03月07日 15:13:57 +00:00Commented Mar 7, 2014 at 15:13
3 Answers 3
I'm not sure to understand the code you wrote, but do you know you can add multiple elements at once with arr.push
?
var array1 = [1, 2, 3, 4, 5];
var array2 = [];
for(var i = 0 ; i < array1.length ; i++) {
array2.push(array1[i], 0, 0, 0);
}
//array2 == [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5, 0, 0, 0]
-
Nice answer. At least this is clear code. Nicely formatted :)Nicky Smits– Nicky Smits2014年03月07日 15:17:27 +00:00Commented Mar 7, 2014 at 15:17
-
+1 - It's not a re-usable like p.s.w.g's answer but it is a simple and clear answer.Mark Walters– Mark Walters2014年03月07日 15:18:55 +00:00Commented Mar 7, 2014 at 15:18
-
@user3242508 I saw what you tried to say (but you tried to edit my answer instead of adding a comment ...). If you want to add
n
zeroes and not strictly three, I need to know how do you determine how many zeroes you want to add ?Sebastien C.– Sebastien C.2014年03月07日 16:00:58 +00:00Commented Mar 7, 2014 at 16:00 -
that is why i use the first function posted beforeuser3242508– user32425082014年03月07日 18:46:08 +00:00Commented Mar 7, 2014 at 18:46
You question is a little hard to follow, but try something like this:
function inject(original, val, times) {
var res = [];
for(var i=0; i < original.length; i++){
res.push(original[i]);
for(var j = 0; j < times; j++){
res.push(val);
}
}
return res;
}
-
I have tried this code, but it doesn`t just put n-zeros(which is the variable times) between the main numbers, but it multiplies the main numbers for "times" times. Hope i was clear.user3242508– user32425082014年03月07日 18:53:42 +00:00Commented Mar 7, 2014 at 18:53
-
@user3242508 Then you must have done something wrong when testing it. See the demonstration; the output is
[1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5, 0, 0, 0]
.p.s.w.g– p.s.w.g2014年03月07日 18:56:04 +00:00Commented Mar 7, 2014 at 18:56
Here's a solution that would work with an arbitrary list and length, and is therefore reusable:
function inject(original, values) {
var result = [];
for (var i = 0 ; i < original.length ; i++) {
result.push(original[i]);
result.push.apply(result, values);
}
return result;
}
console.log(inject([1,2,3], [0,0,0]));
// output: [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0]
console.log(inject([1,1,1], [2,6,2,6]));
// output: [1, 2, 6, 2, 6, 1, 2, 6, 2, 6, 1, 2, 6, 2, 6]
It leverages the the native apply
(here's an explanation) function to execute the push
with an arbitrary list of arguments, defined by the values
array.