Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript js:大型JSON字典&;知道要在飞行中随机显示的条目的较小列表?_Javascript_Templates_Handlebars.js_Mustache - Fatal编程技术网

Javascript js:大型JSON字典&;知道要在飞行中随机显示的条目的较小列表?

Javascript js:大型JSON字典&;知道要在飞行中随机显示的条目的较小列表?,javascript,templates,handlebars.js,mustache,Javascript,Templates,Handlebars.js,Mustache,我刚开始使用mustache.js,我有我的json字典、html模板和mustache.js启动代码来注入整个过程。我的html小胡子模板如下所示: <script id="tpl" type="text/html"> <div class="body"> <p>{{口.zht}}</p> <p>{{口.sam.pyn}}</p> <p>{{口.sam.f

我刚开始使用mustache.js,我有我的json字典、html模板和mustache.js启动代码来注入整个过程。我的html小胡子模板如下所示:

<script id="tpl" type="text/html">
    <div class="body">
        <p>{{口.zht}}</p>
        <p>{{口.sam.pyn}}</p>
        <p>{{口.sam.fra}}</p>
    </div>
</script>
我想从更大的(约1000个条目)JSON字典中选取相应的数据

如何更改或生成变量{{#口}} 及{{/口}} 根据我的焦点列表,我可以用正确的数据打印模板?

//1. Suggest a smaller custom list of keys existing in the larger data. 2. Randomize.
var focusList = ['們','火山口','火'];
var randomKey = focusList[Math.floor(Math.random() * focusList.length)];
//3. variable + Template + variable. 4. printing.
var template = ('{{#'+ randomKey +'}}'+ $('#tpl').html() +'{{/'+randomKey+'}}'); // as {{#火}} ... {{/火}}
var output = Mustache.render(template, myBigJSON);
$('#main').append(output)
//1. Get and create the list of all keys
var focusList = Object.keys(myBigJSON);
模板中是否存在某些类型的变量,例如:

<script id="tpl" type="text/html">
    <div class="body">
        <p>{{{{entry}}.zht}}</p>
        <p>{{{{entry}}.sam.pyn}}</p>
        <p>{{{{entry}}.sam.fra}}</p>
    </div>
</script>

解决方案:给定一个小的键列表,mustache从较大的JSON数据中打印相关条目:

//1. Suggest a smaller custom list of keys existing in the larger data. 2. Randomize.
var focusList = ['們','火山口','火'];
var randomKey = focusList[Math.floor(Math.random() * focusList.length)];
//3. variable + Template + variable. 4. printing.
var template = ('{{#'+ randomKey +'}}'+ $('#tpl').html() +'{{/'+randomKey+'}}'); // as {{#火}} ... {{/火}}
var output = Mustache.render(template, myBigJSON);
$('#main').append(output)
//1. Get and create the list of all keys
var focusList = Object.keys(myBigJSON);
或者1:您也可以使用Object.keys()获取整个条目列表,以从整个列表中打印随机条目:

//1. Suggest a smaller custom list of keys existing in the larger data. 2. Randomize.
var focusList = ['們','火山口','火'];
var randomKey = focusList[Math.floor(Math.random() * focusList.length)];
//3. variable + Template + variable. 4. printing.
var template = ('{{#'+ randomKey +'}}'+ $('#tpl').html() +'{{/'+randomKey+'}}'); // as {{#火}} ... {{/火}}
var output = Mustache.render(template, myBigJSON);
$('#main').append(output)
//1. Get and create the list of all keys
var focusList = Object.keys(myBigJSON);

最后,你可以系统地打印每个条目,而不是随机循环重复一些条目。

你能在小胡子看到任何东西之前切掉你想要的部分吗?小胡子通常的方法是在小胡子看到数据之前对所有数据进行按摩。我是一名博士语言学家,正在做pJS/HTML/CSS中的概念屋顶。遗憾的是,我无法用其他语言处理JSON字典。我应该使用focusList=['创建字典的部分副本吗們','火山口','火'], 然后让mustach.js来处理这个迷你字典?js指示/关键字。是的,数据的部分副本将是常见的mustach方式。你也可以检查一些其他模板引擎(),它们当然都有它们的优点和缺点。我更好地理解。谢谢Mu。有一种方法!疯狂,没有胡子,但工作正常。