I have an outer JS array 'main_category' which should contain several arrays within it called 'temp'.
The array temp contains several string elements taken from an li tag.
In order to populate both the arrays I am running a for-loop through each li tag for each ul tag in the html page, as shown below:
function a{
var category_list = [];
var temp;
//how to split this list iteration into two for-loops
$(".ul_list li").each(function(){
temp = [];
//adding only those list elements of a ul, that are red in color
if($(this).attr("style") == "color:red"){
var cat = $(this).text();
cat = cat.replace(/\s\s+/g, ' '); //removes whitespaces
temp.push(cat); //creates a single array for a single element
}
}
category_list.push(temp);
});
}
Desired output:
category_list = [['l1', 'l2', 'l3'], ['l1', 'l2', 'l3'], ['l1', 'l2', 'l3']..]
Current output:
category_list = [['li'], ['l2'], ['l3']...]
However, the temp.push(cat) LOC creates a temp array for each individual li element instead of creating a temp=[] for all the li elements within one ul.
.ul_list? Also it looks like an invalid function definition forfunction a(){}tempto the category array in a scope wheretempis not defined. The expressioncategory_list.push(temp)needs to be moved one line up..ul_list liwill select a flat list of<li>elements. The elements will not get magically grouped by their parent<ul>element.