获取其子属性包含值的对象(Get object whose sub

编程入门 行业动态 更新时间:2024-10-24 08:20:52
获取其子属性包含值的对象(Get object whose sub-property contains value)

楷模:

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.

更多推荐

本文发布于:2023-08-06 18:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1452864.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:其子   属性   对象   object

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!