javascript - Javascript 数组中的动态串联

标签 javascript arrays dynamic concatenation

假设我有一个数据数组和一个变量(或多个变量)

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/

相关文章:

c# - 调用dll的方法时无法解构动态对象

c# - 确定 DynamicObject 成员访问的预期类型

javascript - 动态内容的 JQuery Find() .value()

javascript - 回调和 promise 有什么区别

javascript - Vue.js:延迟地一次输出一个元素的数组内容

javascript - For循环切换部分功能

c - 数组 b[] 的值如何用另一个函数的另一个数组 a[] 值填充?

arrays - Swift 闭包数组 - 查找元素

javascript - 检查选择器是否包含 jQuery 中的文本(即不是媒体)

javascript - 浏览器图像渲染