javascript Backbone.js - 如何从集合中获取对象文字数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15868979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:22:42  来源:igfitidea点击:

Backbone.js - How to get an array of object literals out of a collection?

javascriptbackbone.jsbackbone.js-collections

提问by professormeowingtons

I'm trying to keep an array of object literals in sync with server data. These objects are being placed on a Google map via backbone.googlemaps extension.

我正在尝试使一组对象文字与服务器数据保持同步。这些对象通过backbone.googlemaps 扩展被放置在谷歌地图上。

I have a collection:

我有一个集合:

var LocationList = Backbone.Collection.extend({ model: Location, url: '/locations' })

var LocationList = Backbone.Collection.extend({ model: Location, url: '/locations' })

How can I grab an array of object literals from this LocationListcollection? My goal is such:

如何从此LocationList集合中获取一组对象文字?我的目标是这样的:

[{name: "Home", address: "123 Pleasant St"}, {name: "Work", address: "123 Unpleasant St"}]

[{name: "Home", address: "123 Pleasant St"}, {name: "Work", address: "123 Unpleasant St"}]

回答by Cubed Eye

You are looking for the .toJSON()method of the collection, see here:

您正在寻找.toJSON()集合的方法,请参见此处

var locations = new LocationList();

locations.toJSON();

回答by Eru

use underscore.js pluck method: http://underscorejs.org/#pluck

使用 underscore.js pluck 方法:http: //underscorejs.org/#pluck