简体   繁体   English

Backbone.js:从集合构建JSON数组

[英]Backbone.js: Build JSON array from collection

I have a backbone collection Platforms . 我有一个骨干收集Platforms The structure of Platforms looks like this: Platforms的结构如下所示:

Platforms
  PlatformList
    models
      0: Platform
        attributes
          id: 1
          name: "some name"
      1: Platform
        attributes
          id: 2
          name: "some other name"

I need to extract the attributes from the models within the collection, and build a JSON array in the following format: 我需要从集合中的模型中提取属性,并使用以下格式构建JSON数组:

[{"id":1,"name":"some name"},{"id":2,"name":"some other name"}]

Calling Platforms.models.toJSON() or JSON.stringify(Platforms.models) results in a literal string "[[object Object], [object Object]]" 调用Platforms.models.toJSON()JSON.stringify(Platforms.models)会生成文字字符串"[[object Object], [object Object]]"

How can I build the JSON array that I need from this collection? 如何从此集合构建我需要的JSON数组?

you should not do platforms.models.toJSON() but instead call toJSON on the collection itself! 你不应该做platform.models.toJSON(),而是在集合本身上调用toJSON!

Platforms.toJSON()

check the following jsfiddle on this solution http://jsfiddle.net/saelfaer/TP9NE/2/ 检查以下jsfiddle这个解决方案http://jsfiddle.net/saelfaer/TP9NE/2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM