How to create an array dynamically in jquery [Resolved]

Posted by Klbaiju under jQuery on 8/22/2016 | Points: 10 | Views : 262 | Status : [Member] | Replies : 2
Hi,

I want to create an array using jquery

like

var totals;

totals = [0, 0, 0, 0, 0, 0];


i want to create this dynamically

for example

var k=3

then

totals=[0,0,0] and so on.

my requirement is like this

var k=5

for( k;k<5;k++)

{

}

finally

totals should be

totals[0,0,0,0,0]


how to do this in jquery

Regards

Baiju




Responses

Posted by: A2H on: 8/22/2016 [Member] [MVP] Bronze | Points: 50

Up
0
Down

Resolved
You can try with the below code

<script>
$(document).ready(function () {
var k = 5;
var arrayval = new Array(k);
for (k = 0; k < 5; k++) {
arrayval.push('0');
}
});
</script>


Thanks,
A2H
Please mark my reply as answer if it helps to resolve your problem

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Klbaiju on: 8/23/2016 [Member] Starter | Points: 25

Up
0
Down
<script>
$(document).ready(function () {
var k = 5;
var arrayval = new Array(k);
for (k = 0; k < 5; k++) {
arrayval.push(0);
}
});
</script>

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response