简体   繁体   English

如何添加到数组中的数组 JavaScript

[英]How to add to an array in an array JavaScript

I have an array:我有一个数组:

["1", "2","3","4","5"]

that I would like to change to:我想改为:

[["1, 0 ,0 ,0"], ["2, 0, 0, 0"],["3,0,0,0"],["4,0,0,0"],["5,0,0,0"]]

I've tried to achieve this with the following code:我尝试使用以下代码来实现这一点:

var arr1 = ["1", "2","3","4","5"];
var arr2 = [,"0", "0","0"];

for(var z=0; z<arr1.length; z++)
    {
    arr1[z] = arr1[z].concat(arr2);
    console.log(arr1)
    }
 

However, this doesn't achieve what I want and places commas between each item, which I think is because it is not a string?但是,这并没有达到我想要的效果,并且在每个项目之间放置了逗号,我认为这是因为它不是字符串? I've tried using.join too but I couldn't get that to work either.我也尝试过 using.join ,但我也无法让它工作。 Could someone point me in the right direction for this please?有人可以为我指出正确的方向吗? Thanks.谢谢。

Please try the following example请尝试以下示例

 const array = ["1", "2", "3", "4", "5"]; const output = array.map((entry) => [`${entry}, 0, 0, 0`]); console.log(output);

See

if it's set in stone, you can do如果它是一成不变的,你可以做

let arr = []

var arr1 = ["1", "2","3","4","5"];
var arr2 = ["0", "0","0"];

arr1.forEach(el=>arr.push([el, ...arr2]));


console.log(arr)

You first need to convert each element to array and then append the second array to the newly converted array element.您首先需要将每个元素转换为数组,然后将第二个数组 append 转换为新转换的数组元素。 The following code snippet would make it clear -下面的代码片段可以清楚地说明 -

 var arr1 = ["1", "2", "3", "4", "5"]; var arr2 = [ "0", "0", "0"]; for (var z = 0; z < arr1.length; z++) { // First convert the element into a list arr1[z] = [arr1[z]]; // Then append the second array to it arr1[z] = arr1[z].concat(arr2); // Another way - //arr1[z].push(...arr2); } // Final array - console.log(arr1);


Using Map():使用地图():

Another way to do it would be using map() as below -另一种方法是使用map()如下 -

 var arr1 = ["1", "2", "3", "4", "5"]; var arr2 = [ "0", "0", "0"]; arr1 = arr1.map(el=>[el,...arr2]); console.log(arr1);


Using ForEach()使用 ForEach()

You could also do it using forEach() method -您也可以使用forEach()方法来做到这一点 -

 var arr1 = ["1", "2", "3", "4", "5"]; var arr2 = [ "0", "0", "0"]; // this will note mutate original arr1 and store result in res let res = []; //... is the spread operator which will allows iterable objects to be expanded in place arr1.forEach(el=>res.push([el,...arr2])); // final result console.log(res);


There's probably numerous other ways as well... These were just few I have listed.可能还有许多其他方法......这些只是我列出的几个。 You could even do it using naive nested loops method if you don't know anything about map() , forEach() , or concat() / push() methods.如果您对map()forEach()concat() / push()方法一无所知,您甚至可以使用简单的嵌套循环方法来做到这一点。

Hope this helps !希望这可以帮助 !

Use map to go through each of your element and append the array with zeros, transform it to a string and wrap in an array again which will hold one value通过每个元素使用 map 到 go 和 append 数组与零,将其转换为字符串并再次包装在一个数组中,该数组将保存一个值

 const a = ["1", "2", "3", "4", "5"] const arr = [0, 0, 0, 0]; const res = a.map(x => [ [x, ...arr].join() ]) console.log(res)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM