admin管理员组文章数量:1435859
I have an HTML definition list, and I want to use jQuery to turn that into a nested JavaScript array. What's the simplest way to do this? Thanks for your help.
My input looks like this:
<dl>
<dt>A</dt>
<dd>A1</dd>
<dt>B</dt>
<dd>B1</dd>
<dd>B2</dd>
</dl>
I want my output to look like this:
[['A', 'A1'], ['B', 'B1', 'B2']]
I have an HTML definition list, and I want to use jQuery to turn that into a nested JavaScript array. What's the simplest way to do this? Thanks for your help.
My input looks like this:
<dl>
<dt>A</dt>
<dd>A1</dd>
<dt>B</dt>
<dd>B1</dd>
<dd>B2</dd>
</dl>
I want my output to look like this:
[['A', 'A1'], ['B', 'B1', 'B2']]
Share
Improve this question
asked Nov 17, 2011 at 22:20
AnirvanAnirvan
6,3745 gold badges42 silver badges54 bronze badges
0
5 Answers
Reset to default 4var final_array = [];
$('dl dt').each(function(){
var dt_dds = []; /* array to hold the dt_dds */
dt_dds.push( $(this).text() ); /* push the dt */
dds = $(this).nextUntil('dt'); /* get all dd's until the next dt */
dds.each(function(){ dt_dds.push( $(this).text() )}); /** push all dd's into the array*/
final_array.push( dt_dds );
})
console.log( final_array );
Here's a fiddle.
This isn't very elegant but you can iterate through the child elements of the <dl>
tag, build an array for each set of <dt>
/<dd>
tags and .push
that array into an output array:
//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"]]
A demonstration of this code can be found at: http://jsfiddle/qkjKp/
You could try something like this:
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);
}
});
You can use .map()
to do that:
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();
Demo: http://jsfiddle/LjZDt/1/
More info about that technique here: http://encosia./use-jquery-to-extract-data-from-html-lists-and-tables/
nextUntil(...) will do the trick to get the proper dd
's
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);
fiddle
本文标签: Turning HTML ltdlgt into a nested JavaScript array with jQueryStack Overflow
版权声明:本文标题:Turning HTML <dl> into a nested JavaScript array with jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745673598a2669715.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论