循环使用coffeescript中的数据结构

时间:2015-01-02 18:09:55

标签: coffeescript

我有以下数据结构:

{
data: [
    {
        id: 5,
        name: 'ItemOne'
    },
    {
        id: 14,
        name: 'ItemTwo'
    },
    {
        id: 15,
        name: 'ItemThree'
    }
]

}

我想绕过它,但我正在挣扎。我试过了:

for result,value of results
  console.log results
  item = 0
  message = "Component: " + value[item]['name'] + " Status: " + value[item]['status']
  output.push message
  item++

但它只返回一个结果。我明显可以逃脱,但我还需要做什么?

1 个答案:

答案 0 :(得分:0)

您应该遍历.data。以下是一个完整的示例:http://jsfiddle.net/hd4ru1kf/5/


最初在问题中,数据符号也存在问题

您可以从学习CoffeeScript JSON notation here

中受益
kids =
  brother:
    name: "Max"
    age:  11
  sister:
    name: "Ida"
    age:  9

编译为:

kids = {
  brother: {
    name: "Max",
    age: 11
  },
  sister: {
    name: "Ida",
    age: 9
  }
};

在您的示例中,您有,个无效的字符,整个JSON结构都已关闭。你也笨拙地嵌套了JSON,也许你不想要那个?相反,我认为这是您正在寻找的工作版本(您可以使用http://coffeescript.org/来检查它):

data:
    [
        {
            id: 1
            name: "Item one"
        }
        {
            id: 2
            name: "Item two"
        }
        {
            id: 3
            name: "Item three"
        }
    ]
相关问题