gpt4 book ai didi

javascript - Javascript 数组中的动态串联

转载 作者:行者123 更新时间:2023-11-28 13:51:31 26 4
gpt4 key购买 nike

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

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com