楷模:
public class Header{ public int ID; public int someValue; public bool someOtherValue; public List<Trailer> Trailers; } public class Trailer{ public int ID; public Language MyLanguage; public string someDifferentValue; }在我的控制器中,我将多个Header类型的对象移交给我的JavaScript代码。 现在我想通过那些Header-Objects并获得满足几个条件的预告片。 正如我发现的那样,有一个叫做“grep”的JS / jQuery函数,类似于c#/ LINQ .Where()。
假设我想获得ID等于1的Header对象和英语中的对应预告片:
result = jQuery.grep(headerCollection, function(t){ return jQuery.grep(t.Trailers, function(subT){ return subT.Language == Language.English } && t.ID == 1 }所以,如果我说得对,结果[0]应该是我的Header-object和结果[0] .Trailers [0]应该是我对应的英语预告片。
但正如预期的那样,它不起作用,我无法弄清楚原因。 有没有更好的方法让这个工作?
Models:
public class Header{ public int ID; public int someValue; public bool someOtherValue; public List<Trailer> Trailers; } public class Trailer{ public int ID; public Language MyLanguage; public string someDifferentValue; }In my Controller I hand over multiple objects of type Header to my JavaScript Code. Now I want to go through those Header-Objects and get those Trailers in which several conditions are met. As I found out already there is a JS/jQuery function called "grep" which is used similar to the c# / LINQ .Where().
Let's say I want to get the Header-object where ID equals 1 and the correspondig Trailer in english language:
result = jQuery.grep(headerCollection, function(t){ return jQuery.grep(t.Trailers, function(subT){ return subT.Language == Language.English } && t.ID == 1 }so if I got it right result[0] should be my Header-object and result[0].Trailers[0] should be my corresponding Trailer with English Language.
But as expected it's not working and I couldn't figure out why. Is there a better way to get this working?
最满意答案
jQuery.grep()返回一个总是真实的数组。
因此,在回调中调用grep实际上没有任何意义。
相反,你应该在数组上调用内置的some()方法 ,这样做你真正想要的。
jQuery.grep() returns an array, which is always truthy.
Therefore, calling grep in the callback doesn't actually make any sense.
Instead, you should call the built-in some() method on the array, which does what you actually want.
更多推荐
发布评论