JavaScript: For loop to build multidimensional array automatically

JavaScript: For loop to build multidimensional array automatically  #angularjs #reactjs

  • var $ = cheerio.load(htmlContent2); //Regex to filter content based on pattern: var regex1 = /[a-z]\.\s/, regex2 = /[0-9]\./, regex3 = /\([a-z]\)/, regex4 = /\([0-9]\)/; allList = []; var newElements = $(“.elem b”).filter(function () { var newList = []; var item1, item2, item3, item4; newList.push($(this).parent().text()); for (var i = 0, j = newList.length; i < j; i++) { if (regex1.test($(this).text())) { item1 = $(this).text(); allList.push(item1); } if (regex2.test($(this).text())) { item2 = $(this).text(); allList.push(item2); } if (regex3.test($(this).text())) { item3 = $(this).
  • The array nesting level varies based on the source content.
  • Its just checking what level you are on and either pushing or popping an array off a stack to build the structure you want.
  • The tags are at the same level in the source content, which is flat (manually formatted), but I need to create new structured content by nesting the result content properly.
  • I am iterating over the source content to generate an array only that the output array is not in the proper format.

The array nesting level varies based on the source content. I have searched the forum for similar approaches to no avail.

@ng_real_ninja: JavaScript: For loop to build multidimensional array automatically #angularjs #reactjs

The tags are at the same level in the source content, which is flat (manually formatted), but I need to create new structured content by nesting the result content properly. So for example I need a. b. c. (first level), 1.2.3 (second level), (a), (b).. (third level) etc. They may occur anywhere in the content. I am able to get the content based on the pattern right but can’t figure out how to nest the second and third levels arrays as a whole under their respective parent. – Manou 4 hours ago

You were getting close. As long as you only need to test for the 4 conditions you set up as regex this should work fine. Basically, its just checking what level you are on and either pushing or popping an array off a stack to build the structure you want.

var regex1 = /[a-z]\.\s/, regex2 = /[0-9]\./, regex3 = /\([a-z]\)/, regex4 = /\([0-9]\)/; var stack = []; stack.push([]); var newElements = $(“.elem b”).filter(function () { var text = $(this).text(); var lastLevel = 0 var currentLevel; if (regex1.test(text)) { currentLevel = 0; } else if (regex2.test(text)) { currentLevel = 1; } else if (regex3.test(text)) { currentLevel = 2; } else if (regex4.test(text)) { currentLevel = 3; } else { throw Error(‘Unexpected content’); } var arr = stack[stack.length – 1]; if (currentLevel > lastLevel) { var next = []; arr.push(next); stack.push(next); } else if (currentLevel < lastLevel) { stack.pop(); } arr = stack[stack.length - 1]; arr.push(text); }); var allList = stack.pop(); var $ = cheerio.load(htmlContent2); //Regex to filter content based on pattern: var regex1 = /[a-z]\.\s/, regex2 = /[0-9]\./, regex3 = /\([a-z]\)/, regex4 = /\([0-9]\)/; allList = []; var newElements = $(“.elem b”).filter(function () { var newList = []; var item1, item2, item3, item4; newList.push($(this).parent().text()); var level2 = []; var level3 = []; for (var i = 0, j = newList.length; i < j; i++) { if (regex1.test($(this).text())) { if(level2.length > 0) { allList.push(level2); level2=[]; } if(level3.length > 0) { level2.push(level3); allList.push(level2); level3=[]; } item1 = $(this).text(); allList.push(item1); continue; } if (regex2.test($(this).text())) { if(level3.length > 0) { level2.push(level3); allList.push(level2); level3=[]; } item2 = $(this).text(); level2.push(item2); continue; } if (regex3.test($(this).text())) { item3 = $(this).text(); level3.push(item3); continue; } } }); console.log(JSON.stringify(allList));

style array.

var a = [“a. “, “1. “, “2. “, “b. “, “1. “, “(a).”, “(b).”, “2. “, “3.”, “c. “]; var b = [“a. “, [“1. “, “2. “], “b. “, [“1. “, [“(a).”, “(b).”], “2. “, “3.”], “c. “]; var level = function(o) { if (/[a-z]\.\s/.test(o)) return 1; if (/[0-9]\./.test(o)) return 2; if (/\([a-z]\)/.test(o)) return 3; if (/\([0-9]\)/.test(o)) return 4; return 0; }; var shadow = function(arr) { var i, j, arr2 = []; for (i = 0, j = arr.length; i < j; i++) { arr2[i] = level(arr[i]); } return arr2; }; var convert = function(arr) { var ia, ib, temp; var s = shadow(arr); if (arr.length > 1) { for (ia = 0, ib = 0; ia < arr.length; ia = ia + 2) { ib = s.indexOf(s[ia], ia + 1); if (ib > -1 && ib – ia > 1) { temp = arr.slice(ia + 1, ib); arr.splice(ia + 1, ib – ia – 1, convert(temp)); s.splice(ia + 1, ib – ia – 1, []); } } } return arr; };

let x = convert(a); let txt =JSON.stringify(x); ut.writeLog(txt);
ut.assertEqual(x, b, joCompare);

Total: 1, Passed: 1, Failed: 0, Stat: 100% Passed

JavaScript: For loop to build multidimensional array automatically

You might also like More from author

Comments are closed, but trackbacks and pingbacks are open.