如何在 Backbone.js 中合并具有相等值的 JSON 个对象
How to combine JSON objects with equal values within Backbone.js
我正在尝试显示多个 "unread" 警报,但如果organizations id等于unread domainId。
如何在车把 #each
函数中显示结果时实现此目的?
JSON
"alerts" : {
"organizations" : [ {
"id" : 165,
"label" : "Label #1"
}, {
"id" : 170,
"label" : "Label #2"
}],
"unread" : [ {
"id" : 20022,
"domainId" : 170,
"created" : "2015-10-13T16:48:08Z",
}, {
"id" : 20022,
"domainId" : 165,
"created" : "2015-10-13T16:48:08Z",
}]
};
Backbone.js:
context: function() {
var d = this.options.data
return {
welcomeAlerts: d.alerts,
welcomeOrg: d.alerts.organizations.label
}
},
车把//HTMl
{{#each welcomeAlerts.unread}}
<li class="alert-item stripe-list-item">
<h4 class="org">{{welcomeOrg}}</h4>
<h4 class="timestamp">{{created}}</h4>
</li>
{{/each}}
只需事先转换数据
var data = {
"alerts" : {
"organizations" : [ {
"id" : 165,
"label" : "Label #1"
}, {
"id" : 170,
"label" : "Label #2"
}],
"unread" : [ {
"id" : 20022,
"domainId" : 170,
"created" : "2015-10-13T16:48:08Z",
}, {
"id" : 20022,
"domainId" : 165,
"created" : "2015-10-13T16:48:08Z",
}]
}
};
var organizationDict = _.groupBy(data.alerts.organizations, 'id');
var unreadWithOrganizations = _(data.alerts.unread)
.map((message) => {
return {
...message,
organization: organizationDict[message.domainId]
}
})
.value();
并显示您的数据
{{#each unreadWithOrganizations}}
<li class="alert-item stripe-list-item">
<h4 class="org">{{organization.label}}</h4>
<h4 class="timestamp">{{created}}</h4>
</li>
{{/each}}
演示:http://jsbin.com/sadoluhepu/edit?js,console
我已经使用 lodash 库和 es6 做了一个例子。您可能想坚持使用 ES6 和下划线,这可能会有所不同。
我正在尝试显示多个 "unread" 警报,但如果organizations id等于unread domainId。
如何在车把 #each
函数中显示结果时实现此目的?
JSON
"alerts" : {
"organizations" : [ {
"id" : 165,
"label" : "Label #1"
}, {
"id" : 170,
"label" : "Label #2"
}],
"unread" : [ {
"id" : 20022,
"domainId" : 170,
"created" : "2015-10-13T16:48:08Z",
}, {
"id" : 20022,
"domainId" : 165,
"created" : "2015-10-13T16:48:08Z",
}]
};
Backbone.js:
context: function() {
var d = this.options.data
return {
welcomeAlerts: d.alerts,
welcomeOrg: d.alerts.organizations.label
}
},
车把//HTMl
{{#each welcomeAlerts.unread}}
<li class="alert-item stripe-list-item">
<h4 class="org">{{welcomeOrg}}</h4>
<h4 class="timestamp">{{created}}</h4>
</li>
{{/each}}
只需事先转换数据
var data = {
"alerts" : {
"organizations" : [ {
"id" : 165,
"label" : "Label #1"
}, {
"id" : 170,
"label" : "Label #2"
}],
"unread" : [ {
"id" : 20022,
"domainId" : 170,
"created" : "2015-10-13T16:48:08Z",
}, {
"id" : 20022,
"domainId" : 165,
"created" : "2015-10-13T16:48:08Z",
}]
}
};
var organizationDict = _.groupBy(data.alerts.organizations, 'id');
var unreadWithOrganizations = _(data.alerts.unread)
.map((message) => {
return {
...message,
organization: organizationDict[message.domainId]
}
})
.value();
并显示您的数据
{{#each unreadWithOrganizations}}
<li class="alert-item stripe-list-item">
<h4 class="org">{{organization.label}}</h4>
<h4 class="timestamp">{{created}}</h4>
</li>
{{/each}}
演示:http://jsbin.com/sadoluhepu/edit?js,console
我已经使用 lodash 库和 es6 做了一个例子。您可能想坚持使用 ES6 和下划线,这可能会有所不同。