假设我有一个数据数组和一个变量(或多个变量)
var i = 1;
var arr = {
1: ['whatever' + i,'there are ' + i + ' dogs in the yard.', etc],
}
有没有办法稍后在函数中动态更新数组中的变量?
所以
function start() {
i++;
alert(arr[1][0]);
}
将输出“whatever2”而不是“whatever1”
最佳答案
你可以有一个数组和你需要的push()
东西,但是当字符串被创建时,它不会再改变。
var array = ['whatever',i,'there are ',i,' dogs in the yard.'];
array.push('more stuff');
array.push('even more stuff');
var string = array.join('')
//string = 'whatever[whatever "i" is during join]there are[whatever "i" is during join]dogs in the yard.more stuffeven more stuff'
关于javascript - Javascript 数组中的动态串联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10526290/