P
P
partyzanx2020-02-23 08:37:07
JavaScript
partyzanx, 2020-02-23 08:37:07

How to create a recursion in pug with creating a list from an object?

Given an object

{
  "label": "蟆",
  "children": [
    {
      "label": "虫"
    },
    {
      "label": "莫",
      "children": [
        {
          "label": "艹"
        },
        {
          "label": "旲",
          "children": [
            {
              "label": "日"
            },
            {
              "label": "大"
            }
          ]
        }
      ]
    }
  ]
}


Theoretically, this object can be shorter, longer, or even empty.
This object reveals what small hieroglyphs a large hieroglyph consists of.
And further to the end of which smaller hieroglyphs a small hieroglyph consists.

Passing data from node.js to pug

Should work

<ul>
    <li><ul>
        <li></li> 
        <li><ul>
              <li></li> 
              <li><ul>
                      <li></li> 
                      <li></li> 
                      </ul>
                  </li> 
              </ul>
          </li> 
       </ul>
    </li>
</ul>

https://jsfiddle.net/xreider/j4bmhv3z/2/

I feel it smells fried) And it's time to move to ejs. But maybe someone can help?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Shannon, 2020-02-23
@partyzanx

For example, like this, as an option

-
 var obj = {
  "label": "蟆",
  "children": [
    {
      "label": "虫"
    },
    {
      "label": "莫",
      "children": [
        {
          "label": "艹"
        },
        {
          "label": "旲",
          "children": [
            {
              "label": "日"
            },
            {
              "label": "大"
            }
          ]
        }
      ]
    }
  ]
 }

mixin unpackKanji(obj)
  if(Array.isArray(obj))
    each item in obj
      +unpackKanji(item)
  else
    ul
      if(obj.children)
        li=obj.label
            +unpackKanji(obj.children)
      else
          li=obj.label

+unpackKanji(obj)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question