我想使用_.where
下划线函数,但我似乎无法让它在对象数组上工作。我有以下json:
var Planets = [
{name:"maths", percComp: "2", preReq: "english"},
{name:"english", percComp: "20", preReq: "geog"},
{name:"german", percComp: "20", preReq: "english"},
{name:"history", percComp: "20", preReq: "german"},
{name:"irish", percComp: "20", preReq: "geog"},
{name:"geog", percComp: "20", preReq: ""},
{name:"french", percComp: "20", preReq: "spanish"},
{name:"spanish", percComp: "57", preReq: "french"}
];
然后我使用以下代码将每个代码添加到数组中:
$(jQuery.parseJSON(JSON.stringify(Planets))).each(function(){
var planet = new Class.Planet(this, paper);
universe.push(planet);
});
我正在尝试使用下划线从列表中获取项目,如下所示:
var planets = _.where(universe, {name: "maths"});
但我收到以下脚本错误:
Uncaught TypeError: Object function (a){return new j(a)} has no method 'where'
编辑 你也可以使用类似的东西
var planets = _.filter(Planets, function(p){ return p.name === "maths"});