简体   繁体   English

打字稿 - 使用“for of”迭代会产生错误“TypeError: element is not a function”

[英]Typescript - using the "for of" iternation creates the error "TypeError: element is not a function"

I have a basic function that should iterate through the values of an array and filter based on whether the element values exist..我有一个基本函数,它应该根据元素值是否存在遍历数组和过滤器的值。

The array has multiple depths:该数组有多个深度:

   public menuList(): any {
        return [
            // SCHEDULER
            {
                route: ["", "scheduler"],
                name: "scheduler",
                moduleId: PLATFORM.moduleName("../components/scheduler/scheduler"),
                title: "scheduler",
                nav: true,
                settings: {
                    icon: "user",
                    roles: ["Employee", "Admin"],
                    pos: "left"
                }
            },

            // CLIENTS
            {
                route: "clients",
                name: "clients",
                moduleId: PLATFORM.moduleName("../components/clients/clientList/clientList"),
                title: "Clients",
                nav: true,
                settings: {
                    icon: "user",
                    roles: ["Employee", "Admin"],
                    pos: "left",
                    nav: [
                        {
                            route: "clients/ClientsList",
                            name: "clientList",
                            moduleId: PLATFORM.moduleName("../components/clients/clientList/clientList"),
                            href: "#clients/clientsList",
                            title: "Client List",
                            settings: {
                                icon: "list",
                                roles: ["Employee", "Admin"],
                            }
                        },
                        {
                            settings: {
                                roles: ["Employee", "Admin"],
                                divider: true,
                            }
                        },
                        {
                            route: "clients/create",
                            name: "newClient",
                            moduleId: PLATFORM.moduleName("../components/clients/newClient/newClient"),
                            href: "#clients/Create",
                            title: "Create Client",
                            settings: {
                                icon: "user",
                                roles: ["Employee", "Admin"],
                            }
                        }
                    ]
                }
            },
        ]
    }
}

This array has just two elements made up of subelements and in the client's element's case three elements under the "nav".这个数组只有两个由子元素组成的元素,在客户端元素的情况下,“nav”下有​​三个元素。

I am trying to construct a function that will traverse every level and remove those that doent satisfy the condition.我正在尝试构建一个函数,该函数将遍历每个级别并删除那些不满足条件的函数。

I started with this:我从这个开始:

public userMenu(userName: string, userRole: string): any {

    var requiredElements = ["route", "name", "moduleId", "title", "nav"];

    for (let element of this.menuList()) {
        var filteredArray = element.filter(function (itm) {
            return requiredElements.indexOf(itm.requiredElements) > -1;
        })

    }
}

However I am getting an error on "element" on the line var filteredArray = element.filter(function (itm) { .但是,我在var filteredArray = element.filter(function (itm) {行上的“元素”上遇到错误。

Its saying in the console:它在控制台中说:

menu2.ts:122 Uncaught TypeError: element.filter is not a function at Menu.73.Menu.userMenu (menu2.ts:122) menu2.ts:122 Uncaught TypeError: element.filter is not a function at Menu.73.Menu.userMenu (menu2.ts:122)

Which is that line..哪条线..

How do I use the "for of" on the "menuList() and then be able to filter on it? in this case its saying that element is not something I can iterate over - or filter etc....我如何在“menuList()”上使用“for of”然后能够对其进行过滤?在这种情况下,它说元素不是我可以迭代的东西 - 或过滤器等......

EDIT As suggested I changed this to:编辑按照建议,我将其更改为:

        public userMenu(userName: string, userRole: string): any {

    var requiredElements = ["route", "name", "moduleId", "title", "nav"];

    this.menuList().forEach(function (item) {
        var filteredArray = item.filter(function (itm) {
            return requiredElements.indexOf(itm.requiredElements) > -1;
        })

        console.log("filteredArray: ", filteredArray)
    })
}

and still got the same error.并且仍然出现相同的错误。

What is it that is causing filter to fail and how do I iterate over and filter on my array above?是什么导致过滤器失败,我如何迭代和过滤上面的数组?

You're trying to apply the filter to individual objects inside the array.您正在尝试将过滤器应用于数组内的单个对象。 That's not how it works: you're supposed to filter the entire array, not its elements individually.这不是它的工作原理:您应该过滤整个数组,而不是单独过滤其元素。

Secondly, .indexOf() works for arrays.其次, .indexOf()适用于数组。 You can't use it to check for object keys.您不能使用它来检查对象键。 .hasOwnProperty() is for that purpose. .hasOwnProperty()就是为了这个目的。 Below it's used combined with .every() to check that all required keys exist.下面它与.every()结合使用来检查所有必需的键是否存在。

Finally a minor point: since you're using TypeScript, you should avoid var in favor of const and let .最后一点:由于您使用的是 TypeScript,您应该避免使用var以支持constlet

public userMenu(userName: string, userRole: string): any {

    const requiredElements = ["route", "name", "moduleId", "title", "nav"];

    const filteredArray = this.menuList().filter(function (itm) {
        return requiredElements.every(function(key) {
            return itm.hasOwnProperty(key);
        });
    });
}

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

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