开发者

Fill Javascript 2d Array with passed Variable

I have an 2d array created thats 50 by 2 and want to fill it with a passed array. I know the array works and the passed variables. But I can't get the passed variable to fill up the array, it just fills with plain text. Is my syntax wrong?

for (i=0; i <50; i++){
basket[i]=new Array(2); 
}

function addtobasket(itemname, itemv开发者_运维问答alue){
    basket[itemcount][itemcount]='itemname itemvalue;'  
}

TIA!


'itemname itemvalue' will just fill the array with 'itemname itemvalue'

So you need to write:

basket[itemcount][itemcount]=itemname+' '+itemvalue;

Don't forget to put the semicolon AFTER the string.


for (i=0; i <50; i++){
    basket[i]=new Array(2); 
}

function addtobasket(itemname, itemvalue){
    basket[itemcount][itemcount]= itemname + " " + itemvalue;  
}

I believe that's what you want, assuming you're trying to get the items into the array in the format "itemname itemvalue" as in your example code.

The reason you're currently seeing the names of the variables in your array, rather than their values, is that you're using the string literal "itemname itemvalue". Anything within a string literal - that is, inside the quotation marks - is left unchanged when the code executes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜