Z
Z
ZaxapKramer2016-08-11 13:52:29
JavaScript
ZaxapKramer, 2016-08-11 13:52:29

Is there any generic way to iterate over JSON and write to DOM?

I have JSON like this:

{
  title: "Grocery",
  food: ["apple", "banana", "cucumber"]
}

or like this:
{
  title: "Grocery",
  food: [
    {
      fruit: "Apple",
      vegetable: "Cucumber"
    }
    {
      fruits: { "Banana", "Chery" },
      vegetable: "Tomato",
      other: "Cheese"
    }
  ]
}

Something simple and universal is needed - somehow put it all in the DOM or just in a string. For what is now (appears) is very, very confusing: I am writing a template engine for client-side js in several lines (works on replace and RegExp).
Thanks in advance. I will be glad to ideas.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Yarkov, 2016-08-11
@ZaxapKramer

There can be no universal way in principle. You bring JSON to a single structure, and then write a template engine.
By the way, I recently wrote a randomizer here. See if it comes in handy.

E
Eugene 222, 2016-08-11
@mik222

Excuse me, are you kidding me?
https://facebook.github.io/react/
www.ractivejs.org
https://vuejs.org/examples/
https://github.com/Lucifier129/react-lite
https://mustache.github.io/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question