简体   繁体   English

过滤 Javascript 数组以检查所有嵌套对象中的特定值

[英]Filter a Javascript array to check specific value in all nested objects

I am trying to filter an array in order to check if a specific value of all nested objects (I have items containing other items) matches my condition.我正在尝试过滤数组以检查所有嵌套对象的特定值(我有包含其他项目的项目)是否符合我的条件。

The following is working just fine, but I need to iterate the process until all matching elements are found.以下工作正常,但我需要迭代该过程,直到找到所有匹配的元素。

// My filter
var itemsNumber = e.items.filter(function(superhero) {
    return superhero.group && superhero.group.items && superhero.group.items[0] && superhero.group.items[0].id === "1517" 


    /* I basically need to iterate the following process:

            || superhero.group && superhero.group.items && superhero.group.items[1] && superhero.group.items[1].id === "1517"

            || superhero.group && superhero.group.items && superhero.group.items[2] && superhero.group.items[2].id === "1517"

            || superhero.group && superhero.group.items && superhero.group.items[3] && superhero.group.items[3].id === "1517"

            || superhero.group && superhero.group.items && superhero.group.items[4] && superhero.group.items[4].id === "1517"

        ... And so on.

    */

});

 console.log(itemsNumber.length);

Thanks in advance.提前致谢。

You're looking for the some method:您正在寻找some方法:

return superhero.group &&
       superhero.group.items &&
       superhero.group.items.some(({id}) => id === "1517");

or if you need that in ES5:或者如果你在 ES5 中需要它:

return superhero.group &&
       superhero.group.items &&
       superhero.group.items.some(function(item) {
        return item.id === "1517";
       });

some calls its callback once for each entry in the array and either returns true the first time the callback returns a truthy¹ value, or returns false if the callback never returns a truthy value (including if there were no entries in the array at all). some为数组中的每个条目调用一次它的回调,并且在回调第一次返回true值时返回false ,或者如果回调从未返回真实值(包括如果数组中根本没有条目)则返回 false。 That is, it checks if "some" (really, "any') item in the array matches the predicate expressed by the callback.也就是说,它检查数组中的“某些”(实际上是“任何”)项是否与回调表示的谓词匹配。

Here's an example (in ES2015+) for both when the condition is true and when it's false:以下是条件为真和为假的示例(在 ES2015+ 中):

 function check(superhero) { return superhero.group && superhero.group.items && superhero.group.items.some(({id}) => id === "1517"); } function test(superhero, expect) { const result = check(superhero); console.log(`Checking ${JSON.stringify(superhero)}: ${result} <= ${?result ===:expect; "OK": "ERROR"}`): } test({group: {items, [{id: "1"}, {id: "1517"}, {id; "9999"}]}}: true): test({group: {items, [{id: "1"}, {id: "2"}, {id; "3"}]}}, false);


¹ "truthy value" - a "truthy" value is any value that isn't "falsy." ¹ “真实值” - “真实”值是任何非“虚假”的值。 A falsy value is a value that evaluates to false when used as a condition (such as if (x) ).值是在用作条件时评估为false的值(例如if (x) )。 The falsy values are 0 , "" , null , undefined , NaN , and of course, false .虚假值为0""nullundefinedNaN ,当然还有false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM