Javascript jQuery-从2列表创建数组

Javascript jQuery-从2列表创建数组,javascript,jquery,arrays,Javascript,Jquery,Arrays,我有一个2列表,我想使用jQuery将单元格转换成数组。我现在已经可以使用了,但是我希望数组也是“2列”,不确定这是否是正确的术语。但基本上我希望每行的2个单元格是数组中同一“行”的一部分。目前我有: $(函数(){ var-arr=[]; $('tbody tr')。每个(函数(){ 变量$this=$(this), 单元格=$this.find('td'); 如果(单元格长度>0){ cell.each(函数(){ arr.push($(this.text()); }); } }); 控制

我有一个2列表,我想使用jQuery将单元格转换成数组。我现在已经可以使用了,但是我希望数组也是“2列”,不确定这是否是正确的术语。但基本上我希望每行的2个单元格是数组中同一“行”的一部分。目前我有:

$(函数(){
var-arr=[];
$('tbody tr')。每个(函数(){
变量$this=$(this),
单元格=$this.find('td');
如果(单元格长度>0){
cell.each(函数(){
arr.push($(this.text());
});
}
});
控制台日志(arr);
});

表格标题
苹果
红色
香蕉
黄色的
橘子
橙色
黄瓜
绿色

您可以这样做

$(函数(){
var arr=$('tbody tr')。get()//将jquery对象转换为数组
.map(功能(行){
返回$(行).find('td').get()
.map(函数(单元格){
返回cell.innerHTML;
})。加入(‘,’);
});
控制台日志(arr);
});

表格标题
苹果
红色
香蕉
黄色的
橘子
橙色
黄瓜
绿色

您可以这样做

$(函数(){
var arr=$('tbody tr')。get()//将jquery对象转换为数组
.map(功能(行){
返回$(行).find('td').get()
.map(函数(单元格){
返回cell.innerHTML;
})。加入(‘,’);
});
控制台日志(arr);
});

表格标题
苹果
红色
香蕉
黄色的
橘子
橙色
黄瓜
绿色

好的,您也可以这样做

$(函数(){
var-arr=[];
flag=0;
$('tbody tr td')。每个(函数(){
如果(标志==0){
arr1=[];
arr1.push($(this.text());
arr.push(arr1);
flag=1;
}否则{
设arr1=arr[arr.length-1];
arr1.push($(this.text());
arr[arr.length-1]=arr1;
flag=0;
}
});
控制台日志(arr);
});

表格标题
苹果
红色
香蕉
黄色的
橘子
橙色
黄瓜
绿色

好的,您也可以这样做

$(函数(){
var-arr=[];
flag=0;
$('tbody tr td')。每个(函数(){
如果(标志==0){
arr1=[];
arr1.push($(this.text());
arr.push(arr1);
flag=1;
}否则{
设arr1=arr[arr.length-1];
arr1.push($(this.text());
arr[arr.length-1]=arr1;
flag=0;
}
});
控制台日志(arr);
});

表格标题
苹果
红色
香蕉
黄色的
橘子
橙色
黄瓜
绿色
我建议:

// using Array.from() to convert the Array-like NodeList returned
// from document.querySelectorAll() into an Array, in order to use
// Array.prototype.map():
let array = Array.from(document.querySelectorAll('tbody tr')).map(

  // tr: a reference to the current array-element of the Array over
  // which we're iterating; using Arrow function syntax:
  (tr) => {

    // here we return the result of the following expression;
    // again using Array.from() to convert the NodeList of
    // the <tr> element's children into an Array, again in order
    // to utilise Array.prototype.map():
    return Array.from(tr.children).map(

      // cell is a reference to the current Node of the Array
      // of Nodes over which we're iterating; here we implicitly
      // return the textContent of each <td> ('cell') Node; after
      // using String.prototype.trim() to remove leading/trailing
      // white-space:
      (cell) => cell.textContent.trim()
    );
  });

表格标题
苹果
红色
香蕉
黄色的
橘子
橙色
黄瓜
绿色
我建议:

// using Array.from() to convert the Array-like NodeList returned
// from document.querySelectorAll() into an Array, in order to use
// Array.prototype.map():
let array = Array.from(document.querySelectorAll('tbody tr')).map(

  // tr: a reference to the current array-element of the Array over
  // which we're iterating; using Arrow function syntax:
  (tr) => {

    // here we return the result of the following expression;
    // again using Array.from() to convert the NodeList of
    // the <tr> element's children into an Array, again in order
    // to utilise Array.prototype.map():
    return Array.from(tr.children).map(

      // cell is a reference to the current Node of the Array
      // of Nodes over which we're iterating; here we implicitly
      // return the textContent of each <td> ('cell') Node; after
      // using String.prototype.trim() to remove leading/trailing
      // white-space:
      (cell) => cell.textContent.trim()
    );
  });

表格标题
苹果
红色
香蕉
黄色的
橘子
橙色
黄瓜
绿色

一个arrya数组怎么样像
[[Apples,red],[banana,Yellow],…]
。你想要/期望什么精确的输出?基本上是@AneesIjaz发布的,所以索引0应该是
['Apples','red']
一个arrya数组怎么样像
[[Apples,red],[banananas,Yellow],…]
。你想要/期望什么样的精确输出?基本上是@AneesIjaz发布的,因此索引0将是
['Apples','Red']
删除
加入(',')
如果你想要数组数组。删除
加入(',')
如果你想要数组数组。