如何登录一个集合中的每个模型的某些领域 [英] How to log a certain field of every model in a collection
问题描述
喜IM从我的收藏试图输出每一个纬度字段。但是什么我做返回结果的正确数量,但他们都表示不确定,数据肯定是存在的,名字是绝对正确的。我用勇气和_.each用得到函数内试图和它曾经说,所有是不确定的。
Hi im trying to output every "lat" field from my collection. However anything i do returns the right number of results but they all say undefined, the data is definitely there and the names are definitely right. I've tried using pluck and _.each with a get inside the function and all it ever says is undefined.
这是当前的方法我尝试
var ccLocal = window.router.carsCollection;
_.each(ccLocal.models, function(model) {
console.log(model.lat);
})
登录 ccLocal
返回它的所有数据,所以它肯定是有整个集合。我究竟做错了什么?
logging ccLocal
returns the entire collection with all its data so its definitely there. What am i doing wrong?
使用 model.get(纬度)
也失败了。
使用的console.log(ccLocal.at(0).attributes);
返回此
Object {unitID: "03_Cow_30", positionHistory: Array[1]}
positionHistory: Array[1]
0: Object
estimatedSpeed: "39"
isToday: false
lastSoundFileName: "F11"
lastSoundRange: "11"
lastSoundTime: "2008-10-29 20:38:25"
lat: "51.466227"
long: "-0.491647"
minutesAgo: 1016726
status: "1"
time: "2011-07-13 16:03:37"
__proto__: Object
length: 1
__proto__: Array[0]
unitID: "03_Cow_30"
__proto__: Object
推荐答案
啊,所以你的模型属性
数据结构是不是大家想象。根据您的属性
结构,你需要这样的事情。这是一个有点脆弱,由于假设positionHistory是至少有一个元素的数组,而这也正是您的数据。
Ah, so your model attributes
data structure is not what everyone thought. Based on your attributes
structure, you need something like this. It's a bit fragile due to assuming positionHistory is an array with at least one element, but that's where your data is.
var ccLocal = window.router.carsCollection;
_.each(ccLocal.models, function(model) {
console.log(model.get('positionHistory')[0].lat);
})
这篇关于如何登录一个集合中的每个模型的某些领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!