转向HTML<;dl>;使用jQuery将其放入嵌套的JavaScript数组中

转向HTML<;dl>;使用jQuery将其放入嵌套的JavaScript数组中,javascript,jquery,html,Javascript,Jquery,Html,我有一个HTML定义列表,我想使用jQuery将其转换为一个嵌套的JavaScript数组。最简单的方法是什么?谢谢你的帮助 我的输入如下所示: <dl> <dt>A</dt> <dd>A1</dd> <dt>B</dt> <dd>B1</dd> <dd>B2</dd> </dl> [['A', 'A1'], ['B', 'B

我有一个HTML定义列表,我想使用jQuery将其转换为一个嵌套的JavaScript数组。最简单的方法是什么?谢谢你的帮助

我的输入如下所示:

<dl>
  <dt>A</dt>
   <dd>A1</dd>
  <dt>B</dt>
   <dd>B1</dd>
   <dd>B2</dd>
</dl>
[['A', 'A1'], ['B', 'B1', 'B2']]
var a = [];
var b = [];

var dlc = $("dl").children();

dlc.each(function (i) {
    if (this.nodeName == "DT") {
       if (b.length) a.push(b);
       b = [$(this).html()]; 
    } else {
       b.push($(this).html());   
    }
    if (i == (dlc.length-1)) {
        a.push(b);
        console.log(a);
    }
});

您可以尝试以下方法:

<dl>
  <dt>A</dt>
   <dd>A1</dd>
  <dt>B</dt>
   <dd>B1</dd>
   <dd>B2</dd>
</dl>
[['A', 'A1'], ['B', 'B1', 'B2']]
var a = [];
var b = [];

var dlc = $("dl").children();

dlc.each(function (i) {
    if (this.nodeName == "DT") {
       if (b.length) a.push(b);
       b = [$(this).html()]; 
    } else {
       b.push($(this).html());   
    }
    if (i == (dlc.length-1)) {
        a.push(b);
        console.log(a);
    }
});

这是一个例子

这不是很优雅,但您可以迭代
标记的子元素,为每组
/
标记和
构建一个数组。将该数组推送到输出数组中:

//setup two arrays, one as a final output and one that will hold each sub-array
var output = [],
    temp   = [];

//iterate through each child element of the `<dl>` element
$('dl').children().each(function () {

    //if this element is a `<dt>` tag
    if (this.tagName == 'DT') {

        //if the `temp` array is not empty
        if (0 in temp) {

            //`.push` the `temp` array onto the `output` array
            output.push(temp);
        }

        //add the text of this `<dt>` tag to the `temp` array as the first key (erasing any data that was inside the `temp` array)
        temp = [$(this).text()];
    } else {

        //if the tag found was anything other than a `<dt>` tag (I'm assuming it's a `<dd>` tag) then `.push` its text into the `temp` array
        temp.push($(this).text());
    }
});

//make sure to add the last iteration of the `temp` array to the `output` array
output.push(temp);

//for the structure supplied in the question, the output looks like this: [["A", "A1"], ["B", "B1", "B2"]]
//设置两个数组,一个作为最终输出,另一个保存每个子数组
var输出=[],
温度=[];
//遍历``元素的每个子元素
$('dl').children().each(函数(){
//如果此元素是``标记
如果(this.tagName==“DT”){
//如果'temp'数组不是空的
如果(温度为0){
//`。将'temp'数组推送到'output'数组上
输出压力(温度);
}
//将此``标记的文本作为第一个键添加到`temp`数组中(删除`temp`数组中的任何数据)
temp=[$(this.text());
}否则{
//如果找到的标记不是``标记(我假设它是一个``标记),那么`。将其文本推入`temp`数组
临时推送($(this.text());
}
});
//确保将'temp'数组的最后一次迭代添加到'output'数组中
输出压力(温度);
//对于问题中提供的结构,输出如下:[“A”、“A1”]、[“B”、“B1”、“B2”]]
此代码的演示可以在以下位置找到:

您可以使用
.map()
执行此操作:

var array = $('dl dt').map(function() {
  // Get the ['A1'] and ['B1', 'B2']
   var items = $(this).nextUntil('dt', 'dd').map(function() {
    return $(this).text();
  }).get();

  // Prepend the dt's value
  items.unshift($(this).text());

  // Needs to be wrapped in a second array so that .map() doesn't flatten.
  return [ items ];
}).get();
演示:

有关该技术的更多信息,请点击此处:

可以获得正确的
dd

var result = [];

$("dt").each(function() {
    var el = $(this),
        tmp = [el.text()];

    el.nextUntil("dt").each(function() {
        tmp.push($(this).text());
    });

    result.push(tmp);
});

console.log(result);

[“A”、“B”]、[“A1”、“B1”、“B2”]!==[[“A”、“A1”]、[“B”、“B1”、“B2”]
Hah你说得对,john,我没有密切注意,修改了代码:-)这是很棒的、优雅的,也是发布的最短答案。我最终接受了a,因为它让我觉得可读性更强,但如果可以的话,我也会把你的标记为接受。谢谢,戴夫。