Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用集合中包含的索引值将项推送到数组_Javascript_Arrays_Set - Fatal编程技术网

Javascript 使用集合中包含的索引值将项推送到数组

Javascript 使用集合中包含的索引值将项推送到数组,javascript,arrays,set,Javascript,Arrays,Set,假设我有一些空数组array1=[]、array2=[]、array3=[] 以及一些包含在双索引数组中的数据,如sothis.data[row][cell] 我有一组整数,它们的大小可以根据数据和行中每个单元格中的信息而变化。是否有一种方法可以在集合上迭代,使array1包含this.data[row][(SetValue1)],array2包含this.data[row][(SetValue2)]中的所有值。?我正在考虑一个for循环,从集合的值开始迭代,但不确定什么可能是解决这个问题的好方

假设我有一些空数组
array1=[]、array2=[]、array3=[]

以及一些包含在双索引数组中的数据,如so
this.data[row][cell]


我有一组整数,它们的大小可以根据数据和行中每个
单元格中的信息而变化。是否有一种方法可以在集合上迭代,使
array1
包含
this.data[row][(SetValue1)]
array2
包含
this.data[row][(SetValue2)]
中的所有值。?我正在考虑一个for循环,从集合的值开始迭代,但不确定什么可能是解决这个问题的好方法。

将数组放入另一个数组中,这样您就可以在集合中使用索引来引用相应的数组

arrays = [array1, array2, array3];
var index = 0;
newSet().forEach((val) => arrays[index++].push(this.data[row][val]));
(这个答案是用c语言写的,但是看看它,看看你是否能把这个想法运用到javaScript中)如果你想从二维数组中创建一个锯齿状数组,这是我的解决方案:

            public static int[][] MoveToJaggedArray(int[,] a)//defining a function
            {
                int row = a.GetLength(0);//number of rows
                int Length = a.GetLength(1);//Length \ width of the array

                int[][] na = new int[row][];//setting an array of array


                for(int i =0;i<row;i++)//running on each row
                {
                    for (int k = 0; k < length; k++)//run on each cell
                        na[i][k] = a[i, k];//transferring value in array a in location row =i widthPlace =k to to array in location i in array na and within the array widthPlace = k 
                }
               return na;
            }
publicstatic int[]MoveToJaggedArray(int[,]a)//定义函数
{
int row=a.GetLength(0);//行数
int Length=a.GetLength(1);//数组的长度\宽度
int[][]na=new int[row][];//设置数组的数组

对于(int i=0;我不明白您试图做什么。您所说的
SetValue1
SetValue2
等是什么意思?我指的是Set@Barmar因此,如果集合包含值
{4,5,6}
则将
此.data[row][4]
推入
数组1
此.data[row][5]
进入
array2
,并
此.数据[行][6]
into
array3
如果集合包含3个以上的值怎么办?它们是否环绕?谢天谢地,我正在处理的数据不会产生一个包含3个以上值的集合,但理论上,集合中没有任何附加值需要它自己的数组。你确定集合中只有3个元素吗?可以是1-3个集合中的任意一个d、 当您使用
forEach
对它们进行迭代时,索引与值相同。我已将代码更改为使用一个单独的索引变量。什么会让它更清楚?更新后的答案有效吗?这是JavaScript吗?在我看来像Java,我认为原始问题是关于JS的。这是c sharp,但我决定回答,因为我想他可以接受这个想法并将其转移到javascriptyeah不会撒谎这对meI来说没有多大意义添加一些评论,所以再看一看