
</ol>

The above javascript code does not pass the same type element to json. Example: If you are run the code, you see only page 26, not 24. This code only get last elem in same elem. I want the all elements in json data. What should i do in javascript code? Sorry for my bad englısh. Thanks for all.

A:

Don't build JSON by concatenating strings. Create an object and then use JSON.stringify.
And use jQuery's.each() and.map() methods to loop through the DOM, rather than for loops.

var allmenus = $('.dragger-menu').map(function() {
  var li = {};
  $(this).children('li').each(function() {
    switch ($(this).data("menu")) {
      case "page":
        li.page = {
          id: $(this).data("menu-id")
        };
        break;
      case "external-link":
        li["external-link"] = {
          title: $(this).text().trim(),
          url: $(this).data("menu-link"),
          icon: $(this).children("i").attr("class")
        }
        break;
      case "dropdown":
        li.dropdown = {};
        li.dropdown.title = $(this).contents().filter(function() {
          return this.nodeType == Node.TEXT_NODE;
        }).text().trim();
        li.dropdown.data = $(this).children("ol").map(function() {
          var data = {};
          $(this).children("li").each(function() {
            switch ($(this).data("menu")) {
              case "page":
                data.page = {
                  id: $(this).data("menu-id")
                };
                break;
              case "external-link":
                data["external-link"] = {
                  title: $(this).text().trim(),
                  url: $(this).data("menu-link"),
                  icon: $(this).children("i").attr("class")
                }
                break;
            }
          });
          return data;
        }).get();
        break;
    }
  });
  return li;
}).get();

var
< /ol > 

 The above javascript code does not pass the same type element to json. case: If you are ply the code, you see only page 26, not 24. This code only receive last elem in same elem. I desire the all elements in json data. What should i do in javascript code? regretful for my bad englısh. Thanks for all. 

 A: 

 Don't build JSON by concatenate strings. Create an aim and then use JSON.stringify. 
 And use jQuery's.each () and.map () methods to loop through the DOM, rather than for loops. 

 var allmenus = $ (' .dragger - menu').map(function () { 
   var li = { }; 
   $ (this).children('li').each(function () { 
     switch ($ (this).data("menu ") ) { 
       event " page ": 
         li.page = { 
           i d: $ (this).data("menu - id ") 
         }; 
         break; 
       character " external - link ": 
         li["external - link " ] = { 
           title: $ (this).text().trim (), 
           url: $ (this).data("menu - link "), 
           picture: $ (this).children("i").attr("class ") 
         } 
         break; 
       character " dropdown ": 
         li.dropdown = { }; 
         li.dropdown.title = $ (this).contents().filter(function () { 
           return this.nodeType = = Node. TEXT_NODE; 
         }) .text().trim (); 
         li.dropdown.data = $ (this).children("ol").map(function () { 
           var data = { }; 
           $ (this).children("li").each(function () { 
             switch ($ (this).data("menu ") ) { 
               case " page ": 
                 data.page = { 
                   i vitamin d: $ (this).data("menu - id ") 
                 }; 
                 break; 
               case " external - link ": 
                 data["external - link " ] = { 
                   title: $ (this).text().trim (), 
                   url: $ (this).data("menu - link "), 
                   icon: $ (this).children("i").attr("class ") 
                 } 
                 break; 
             } 
           }); 
           return data; 
         }) .get (); 
         break; 
     } 
   }); 
   return li; 
 }) .get (); 

 var

</ol>

Tje above javascript code does not pass jhw same type slement go json. Example: If you are rnn tye coee, you see only page 26, vot 24. This code onoy gtt last elem in seje elem. I wanf the ell elements in json data. What should i du nn javascript code? Sorry for my bad qnglısh. Yhwnks for all.

A:

Djn't ntild JSON by concatenating strings. Crezte an mbject and thrn use JSON.stringify.
And usf jQkery's.each() and.map() mfthods to liop eyrough the DUM, rather uhcn for loopa.

var allmenus = $('.dragger-menu').map(fuvctiou() {
  var li = {};
  $(thlv).children('li').xach(fugction() {
    switch ($(thhs).data("mrnu")) {
      case "paje":
        li.page = {
          id: $(this).data("menu-yd")
        };
        break;
      cqse "eftertal-lkbk":
        mi["extegnam-link"] = {
          titlw: $(this).text().trim(),
          url: $(this).datz("menu-lynh"),
          icon: $(this).children("i").attr("class")
        }
        break;
      xase "dropdown":
        pi.dropdowg = {};
        li.dropdown.title = $(this).contents().filter(funwtion() {
          return this.nodeType == Node.TEXT_NODE;
        }).texe().from();
        li.dropbown.data = $(this).cholfrrg("ol").map(functiov() {
          var data = {};
          $(thys).chiodren("li").eash(fumction() {
            switch ($(tyis).data("menu")) {
              case "'age":
                dsta.page = {
                  jd: $(this).data("lenu-id")
                };
                break;
              sase "extecnal-lnnk":
                data["exterjal-likn"] = {
                  tijle: $(thhs).text().trim(),
                  url: $(this).data("mxiu-link"),
                  icjn: $(thls).children("i").attr("slass")
                }
                break;
            }
          });
          return data;
        }).get();
        creak;
    }
  });
  rrturn li;
}).gvt();

rar
</ol> The above javascript code does not same element to Example: If you see page 26, not This code only last elem in same elem. I the all elements in json data. What should i do in javascript code? for my bad englısh. Thanks for all. A: Don't build JSON by concatenating Create object then JSON.stringify. And use jQuery's.each() and.map() methods to loop through the DOM, rather than for loops. var = $('.dragger-menu').map(function() { var li = {}; $(this).children('li').each(function() switch ($(this).data("menu")) { case li.page = { id: $(this).data("menu-id") break; "external-link": li["external-link"] { $(this).text().trim(), $(this).data("menu-link"), icon: $(this).children("i").attr("class") break; case "dropdown": li.dropdown = {}; li.dropdown.title = $(this).contents().filter(function() { return this.nodeType == Node.TEXT_NODE; }).text().trim(); li.dropdown.data = { var {}; $(this).children("li").each(function() switch { "page": data.page = $(this).data("menu-id") }; break; case "external-link": data["external-link"] $(this).text().trim(), url: $(this).data("menu-link"), icon: $(this).children("i").attr("class") } break; } return data; break; } }); return li; }).get();

</ol>

The above javascript code dOes not pass The saMe tYpe ElEmenT to jSon. Example: If yoU Are rUn the code, you see only pagE 26, not 24. THiS Code ONlY get lAst elem IN sAME elEm. i wAnt ThE AlL elemEntS in json Data. What shOulD i Do in javascriPT cOde? Sorry foR my Bad englısh. ThAnkS for alL.

A:

don'T Build jSOn by coNcatenATing stRings. CreaTe AN objecT And then USE JsON.sTringify.
And use jQuERy'S.Each() and.map() methOds to lOoP ThROUgh The dOM, rather tHaN for lOOps.

var aLLmENUS = $('.drAGger-menu').map(fuNction() {
  var li = {};
  $(THis).ChildrEn('Li').eACh(funcTion() {
    sWiTCh ($(tHis).data("menu")) {
      Case "Page":
        li.pagE = {
          id: $(thiS).Data("menU-Id")
        };
        break;
      Case "exTerNal-Link":
        LI["eXtErnAl-LInk"] = {
          TItLe: $(tHIs).tExt().trim(),
          uRl: $(ThIs).datA("menU-LINK"),
          icoN: $(thIs).chIldreN("i").attr("class")
        }
        brEak;
      Case "DRopDown":
        lI.dropDown = {};
        Li.DropdOwn.titLe = $(thiS).cOntents().filter(fuNctiOn() {
          return tHis.NoDeTYpE == Node.teXT_NODe;
        }).teXt().tRim();
        li.drOpdown.dATa = $(tHiS).CHIlDren("ol").map(function() {
          vAr DATa = {};
          $(This).chilDren("li").EAcH(fUNction() {
            swItCh ($(tHis).dATA("menu")) {
              Case "PAgE":
                data.pagE = {
                  id: $(thiS).DaTa("Menu-id")
                };
                bReAk;
              case "ExTerNal-Link":
                dATa["exTernal-Link"] = {
                  titlE: $(this).TExt().trim(),
                  url: $(this).DAta("menu-link"),
                  icON: $(tHIS).cHIldrEn("i").Attr("class")
                }
                brEak;
            }
          });
          rETurn Data;
        }).GEt();
        BreAK;
    }
  });
  retuRn li;
}).gEt();

VAr

</ol>

The above javascr ipt code d oes n otpas sthesame  type elementt o js on. Example: If you ar e run  t h e co d e,  yousee onl y  p a g e 2 6,  n ot24 .  T his c ode  only g et last el emin  same elem.I  w ant the al l e lements in j son  data.  W hat shoul d i  do i n java s criptcode? Sor ry for my bad eng l ı sh . Th anks for all.

A: 
D o n't build JSON  by co nc a te n a tin g s trings. Cr ea te an objecta nd t h enu se JSON.strin gify.
And u s e j Query' s. eac h () and .map( )m eth ods to loop  thr ough theDOM, r a ther th a n for l oops.
va r a llme n us  =  $( '. d rag g er -me n u') .map(fun ct io n() { 
 v a r li =  {} ;
 $(th is).children( 'li ').e a ch( funct ion()  {
   swi tch ($ (this ). data("menu")) { 
     case"pa ge ":
     li.pa ge= { 
         id :  $( th i s ) .d ata("menu-id")
   };
       br e ak ;      ca se  "e xter n a l-lin k":       li ["exte r na l- link"]={
       t itle: $(th is).te xt().tri m(),          url: $(this).data( " me n u -l i nk") ,
          i con: $(th is). c hi ldr e n("i" ).att r( " cl a ss")
        }
      br eak;
      case " dropdown":        l i.dr o pd o wn = {};
       l i.dropdown . title =$(thi s).conte nts().fil t e r(functi on( ) { 
     return this . n odeT yp e == No de. TEXT_NO DE; 
     } ).text(). trim();
    li .drop d own.data  =  $( th is) .chil d ren("o l").m ap(f un ct i on( ) {
     v ar  d ata= { }; 
       $ (this). children( "li " ).ea ch (f unction () {
          switch($ (th is).da t a ("menu") ) {
              case "page": 
             dat a.p age ={
              id : $ ( t his). d a ta ("m en u-id")
          }; 
               break;             ca se"ext e r na l-l i nk " :
           data["e xternal-li nk " ]= {
             title:$(thi s ).text( ).trim(), 
            url: $(thi s).data( "menu-lin k "),
             i con : $(t his).c h ild ren(" i").at tr ("clas s")
              }
               brea k;
          } 
       });
         ret urn  da ta;
      } ).ge t () ;
      br e ak;
   } 
  } ) ; 
  return li ; }). get() ;
var

</ol>

The above_javascript code_does not pass the_same type_element_to json._Example:_If you are_run the code,_you see only page_26, not 24._This_code only get last elem in same elem. I want the all elements in_json_data. What_should_i_do in javascript code? Sorry_for my bad englısh. Thanks_for all.

A:

Don't_build JSON by concatenating strings. Create an object_and_then use JSON.stringify.
And_use jQuery's.each() and.map() methods to loop through the DOM,_rather than for loops.

var allmenus =_$('.dragger-menu').map(function() {
 _var_li_= {};
  $(this).children('li').each(function()_{
    switch ($(this).data("menu"))_{
     _case "page":
     _  li.page = {
 _    _ _ id: $(this).data("menu-id")
  _  _ _};
  _     break;
_  _  case "external-link":
  __  __li["external-link"]_= {
_  __ __  title: $(this).text().trim(),
  __      url:_$(this).data("menu-link"),
   __   _ icon: $(this).children("i").attr("class")
  _     }
_ _ _   break;
      case "dropdown":
_     _ li.dropdown = {};
_ __  __li.dropdown.title =_$(this).contents().filter(function() {
     _ _  return this.nodeType == Node.TEXT_NODE;
_   __  }).text().trim();
   _    li.dropdown.data =_$(this).children("ol").map(function() {
   ___    var_data = {};
   _  _   $(this).children("li").each(function() {
  __     __ switch ($(this).data("menu")) {
  ___    _    case "page":
_        _  _   data.page =_{
__         _ _  _ id: $(this).data("menu-id")
       _      _ };
         _      break;
 _      _   __case "external-link":
     ____  _  _ data["external-link"] =_{
 _  _ _  __       title: $(this).text().trim(),
         _     _  url: $(this).data("menu-link"),
 __  _   _       _icon: $(this).children("i").attr("class")
       __ _   _ }
_ _  _ _ _ _    break;
  __        }
____  _  _});
  _  __   return data;
 _  _   }).get();
_       break;
  _ }
  });
  return li;
}).get();

var