我这里有一个益智游戏。我有以下收藏:
var TagsCollection = Backbone.Collection.extend({
model: TagModel,
parse : function(response) {
console.log(response);
// code to go
return response;
}
});
现在获取以下示例 JSON 对象:
{
id: 10149682,
published: "2014-01-13 08:23:00",
title: "Title",
tags: "tag1,tag2,tag3,tag4"
}
现在我想要的是插入一些代码,将现有响应重新映射为以下格式:
[{name: "tag1"}, {name: "tag2"}, {name: "tag3"}, {name: "tag4"}]
并将其加载到集合中。 (重要说明 - 使用 Backbone/Underscore 方法 - 例如 _.chain/_.reduce 等)。
最佳答案
您可以 split你 tags
键:
var tags = response.tags.split(',');
和map结果数组
return _.map(tags, function(tag) {
return {name: tag};
});
关于javascript - 一个 JSON 对象分成几个集合对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21094568/