如何在javascript中向字符串添加字符串

时间:2022-02-01 03:46:51

I wanna create an array in javascript which looks like this:

我想在javascript中创建一个如下所示的数组:

[0,0,0,0,0,1,1,0,0,1,1,1,1,0,0],[0,0,0,0,1,1,1,1,0,1,0,1,1,1,0],[0,0,0,0,1,1,1,1,0,1,0,0,1,1,0],[0,0,0,0,0,1,1,0,1,0,1,0,1,0,0]

My problem is that I don't know how to add the opening and closing square brackets to the start and the end of the output string.

我的问题是我不知道如何将开始和结束方括号添加到输出字符串的开头和结尾。

here's my code:

这是我的代码:

game = new Array();
for(row=0;row<matrix.length;++row){ 
        game[row]=matrix[row].join(','); 
    }
    document.getElementById('jsvalue').value=game.join('],[');
    document.getElementById('name2').value = name;

I tried a few things, but they didn't seem to work and all I got were errors or this output:

我尝试了一些东西,但它们似乎没有用,我得到的只是错误或输出:

0,0,0,0,0,1,1,0,0,1,1,1,1,0,0],[0,0,0,0,1,1,1,1,0,1,0,1,1,1,0],[0,0,0,0,1,1,1,1,0,1,0,0,1,1,0],[0,0,0,0,0,1,1,0,1,0,1,0,1,0,0

How could I add them? Is there a simple array method that I missed and would solve my problem?

我怎么能添加它们?是否有一个简单的数组方法,我错过了,并将解决我的问题?

Thanks in advance!

提前致谢!

2 个解决方案

#1


2  

It looks like you are trying to set the value of an HTML element to the format you described in your question. However, you are not setting the value of that HTML element to an Array - you are setting it to a string. the .join function outputs a string. If indeed you want the value to be set to a string formatted in the way you described, then you could take advantage of .join, but have to do a little bit in addition to what you are doing:

看起来您正在尝试将HTML元素的值设置为您在问题中描述的格式。但是,您没有将该HTML元素的值设置为数组 - 您将其设置为字符串。 .join函数输出一个字符串。如果您确实希望将值设置为以您描述的方式格式化的字符串,那么您可以利用.join,但除了您正在执行的操作之外还必须执行一些操作:

    game = new Array();
    for(row=0;row<matrix.length;++row){ 
        game[row]= "[" + matrix[row].join(',') + "]";
    }
    document.getElementById('jsvalue').value=game.join(',');
    document.getElementById('name2').value = name;

#2


0  

If you are using join to create the string, then why not just manually add the brackets? For example: document.getElementById('jsvalue').value= '[' + game.join('],[') + ']';

如果您使用join来创建字符串,那么为什么不只是手动添加括号?例如:document.getElementById('jsvalue')。value ='['+ game.join('],[')+']';

#1


2  

It looks like you are trying to set the value of an HTML element to the format you described in your question. However, you are not setting the value of that HTML element to an Array - you are setting it to a string. the .join function outputs a string. If indeed you want the value to be set to a string formatted in the way you described, then you could take advantage of .join, but have to do a little bit in addition to what you are doing:

看起来您正在尝试将HTML元素的值设置为您在问题中描述的格式。但是,您没有将该HTML元素的值设置为数组 - 您将其设置为字符串。 .join函数输出一个字符串。如果您确实希望将值设置为以您描述的方式格式化的字符串,那么您可以利用.join,但除了您正在执行的操作之外还必须执行一些操作:

    game = new Array();
    for(row=0;row<matrix.length;++row){ 
        game[row]= "[" + matrix[row].join(',') + "]";
    }
    document.getElementById('jsvalue').value=game.join(',');
    document.getElementById('name2').value = name;

#2


0  

If you are using join to create the string, then why not just manually add the brackets? For example: document.getElementById('jsvalue').value= '[' + game.join('],[') + ']';

如果您使用join来创建字符串,那么为什么不只是手动添加括号?例如:document.getElementById('jsvalue')。value ='['+ game.join('],[')+']';