我正在使用优秀的Underscore.js库.我有一个特定的任务,我可以使用JavaScript或jQuery做得很好但是想知道在Underscore中是否有某种抽象可以让我错过了.
基本上我有一个像这样的对象 -
var some_object_array = [{id: "a", val: 55}, {id: "b", val: 1}, {id: "c", val: 45}];
Run Code Online (Sandbox Code Playgroud)
我想把它转换成 -
var some_map = {"a": {id: "a", val: 55}, "b": {id: "b", val: 1}, "c": {id: "c", val: 45}};
Run Code Online (Sandbox Code Playgroud)
我知道我可以用_.groupBy(some_object_array, "id").但这会返回如此的地图 -
var some_grouped_map = {"a": [{id: "a", val: 55}], "b": [{id: "b", val: 1}], "c": [{id: "c", val: 45}]};
Run Code Online (Sandbox Code Playgroud)
请注意,这会做广告宣传的内容.但是我希望自己some_map不用迭代对象.
任何帮助赞赏.
I have a bootstrap dropdown menu below. It has a link in it hooked up to a knockout.js binding, that returns false because I dont want the # tag to be send to the browser url. However doing this doesnt close the dropdown menu when I click the link. Anyway around this?
HTML
<div class="btn-group">
<button class="btn dropdown-toggle" data-toggle="dropdown" data-bind="enable: !noResults()"><i class="icon-download-alt" ></i> Export <span class="icon-caret-down"></span></button>
<ul class="dropdown-menu">
@foreach(var exportUrl in Model.ExportUrls)
{
<li>
<a href="#" data-bind="disable: noResults(), download: { …Run Code Online (Sandbox Code Playgroud) javascript event-propagation twitter-bootstrap knockout.js drop-down-menu
UPDATE Jade v0.24.0使用!=属性语法修复此问题.option(value!='<%= id %>')
我正在尝试<option>使用jade 构建一个,其中选项的值是UnderscoreJS模板标记:<%= id %>但我无法让它工作,因为jade将我的标记文本转换为<= id >.
这是我的Jade标记:
script(id="my-template", type="text/template")
select(id="type")
<% _.each(deviceTypes, function(type){ %>
option(value='<%= type.id %>') <%= type.name %>
<% }) %>
Run Code Online (Sandbox Code Playgroud)
我希望它能产生这个html:
<script id="my-template" type="text/template">
<select id='type'>
<% _.each(deviceTypes, function(type){ %>
<option value="<%= type.id %>"> <%= type.name %> </option>
<% }) %>
</select>
</script>
Run Code Online (Sandbox Code Playgroud)
但我得到的是这样的:
<script id="my-template" type="text/template">
<select id='type'>
<% _.each(deviceTypes, function(type){ %>
<option value="<%= type.id %>"> <%= type.name %> </option>
<% }) …Run Code Online (Sandbox Code Playgroud) 我是Underscore js的新手,对如何使用它感到困惑.我有一个'目标'的集合,我想通过ID找到其中一个.
这是数据:
{"goal":[
{
"category" : "education",
"title" : "Charlie University",
"description" : "Lorem ipsum dolor sit amet",
"date" : "01/03/2020",
"value" : 50000,
"achievability" : 3,
"experimental_achievability": 3,
"suggested": false,
"accounts": [
{
...
},
{
...
}
],
"articles": [
{
...
},
{
...
},
{
...
}
],
"related_goals": [
{
...
}
],
"id":"1"
},
{
"category" : "family",
"title" : "Getting married",
"description" : "Lorem ipsum dolor sit amet",
"date" : "01/03/2022",
"value" …Run Code Online (Sandbox Code Playgroud) 使用Underscore.js,我试图多次对项目列表进行分组,即
然后由SIZE分组为每个SIZE,按类别分组......
http://jsfiddle.net/rickysullivan/WTtXP/1/
理想情况下,我希望有一个函数或扩展,_.groupBy()以便您可以使用参数对其进行分组.
var multiGroup = ['size', 'category'];
Run Code Online (Sandbox Code Playgroud)
可能只是混合...
_.mixin({
groupByMulti: function(obj, val, arr) {
var result = {};
var iterator = typeof val == 'function' ? val : function(obj) {
return obj[val];
};
_.each(arr, function(arrvalue, arrIndex) {
_.each(obj, function(value, objIndex) {
var key = iterator(value, objIndex);
var arrresults = obj[objIndex][arrvalue];
if (_.has(value, arrvalue))
(result[arrIndex] || (result[arrIndex] = [])).push(value);
Run Code Online (Sandbox Code Playgroud)
我的头很痛,但我觉得还有更多需要去的地方......
});
})
return result;
}
});
properties = _.groupByMulti(properties, function(item) {
var testVal = item["size"];
if (parseFloat(testVal)) …Run Code Online (Sandbox Code Playgroud) 如果我在Google Chrome扩展程序中使用underscore.js的_.template(),我会在控制台中收到以下错误:
未捕获的错误:对于此上下文不允许从字符串生成代码
有没有办法克服这个错误?
我正在使用underscore.js的模板库,我不知道如何在模板中使用逻辑.例如,我想在模板中打印一组标签.对此最好的方法是什么?
使用Javascript:
bunny_data = {
name: "sprinkles",
age: 1,
tags: ['fuzzy','wuzzy']
};
bunny_view = $("#bunny-template").html();
$(body).append(_.template(bunny_view,bunny_data));
Run Code Online (Sandbox Code Playgroud)
模板:
<script type='text/template'>
<div>
<h5><% = name %></h5>
<ul class='tag-list'>
<!-- How do I print the tags here? -->
</ul>
</div>
</script>
Run Code Online (Sandbox Code Playgroud) 如何在underscore.js模板中为使用backbone.js?构建的应用程序设置变量?我只想创建可重用的已处理字符串.此外,可以使用underscore.js内置函数_.escape来处理这些变量吗?
<script type="text/html" id="templateresults">
<p><%= encodeURIComponent(title) %></p> // this works
// try 1:
var encodedTitle = encodeURIComponent(title); // shows up as regular text
<p>'+encodedTitle+'</p> // this doesn't work and shows up as regular text
// try 2:
<% var encodedTitle = encodeURIComponent(title); %> // nothing shows up
<p><% '+encodedTitle+' %></p> // nothing shows up
</script>
Run Code Online (Sandbox Code Playgroud)
title 是一个JSON项(文本字符串).
编码输出:This%20is%20a%20Sample%20Title
常规输出:This is a Sample Title
javascript ×5
arrays ×1
comonad ×1
haskell ×1
html-encode ×1
knockout.js ×1
monads ×1
php ×1
pug ×1
tcpdf ×1
templates ×1