Skip to content

Instantly share code, notes, and snippets.

@ryohey
Last active December 4, 2015 17:43
Show Gist options
  • Select an option

  • Save ryohey/a5c90f5c50e756bcb717 to your computer and use it in GitHub Desktop.

Select an option

Save ryohey/a5c90f5c50e756bcb717 to your computer and use it in GitHub Desktop.
convert object to <ul><li> and <dl><dt><dd> in CoffeeScript for React.js
json2html = (obj) ->
if obj instanceof Array
list = json2html o for o in obj
<ul>{list}</ul>
else if obj instanceof Object
list = ([
<dt>{key}</dt>
<dd>{json2html value}</dd>
] for key, value of obj)
<dl>{list}</dl>
else
obj.toString()
@ryohey
Copy link
Copy Markdown
Author

ryohey commented Dec 4, 2015

json2html [
  "hoge",
  "fuga",
  "dict":
    "nuga": "moge"
    "foo": [
      "bar",
      "bar2"
    ]
]
<ul>
  <li>hoge</li>
  <li>fuga</li>
  <li>
    <dl>
      <dt>dict</dt>
      <dd>
        <dl>
          <dt>nuga</dt>
          <dd>moge</dd>    
        </dl>
      </dd>
      <dt>foo</dt>
      <dd>
        <ul>
          <li>bar</li>
          <li>bar2</li>
        </ul>
      </dd>
    </dl>
  </li>
</ul>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment