简体   繁体   English

循环数组返回字符串类型的元素,而不是数组的类型

[英]Loop array returns element of type string instead of array's type

I have problems looping a TypeScript array. 我在循环TypeScript数组时遇到问题。 These are the methods: 这些是方法:

getNotification(evt: string, rowIndex: number) {
    console.log("Production order: law has changed to " + evt + " " + rowIndex);
    var select = document.getElementsByName("ProductId-" + rowIndex);

    this.removeOptions(select);

    if ((evt != null) && (evt != "")) {
        let productsByLaw: IProduct[];

        productsByLaw = this.products.filter(x => x.lawId == +evt);
        for (let product in productsByLaw) {
            select.options[select.options.length] = new Option(product.name, product.productid);
        }
    }

}

removeOptions(selectbox : any) {
    var i;
    for (i = selectbox.options.length - 1; i >= 0; i--) {
        selectbox.remove(i);
    }
}

I don't know why this Option(product.name, product.productid) throw this error: 我不知道为什么这个Option(product.name, product.productid)抛出此错误:

Error TS2339 (TS) Property 'name' does not exist on type 'string'. 错误TS2339(TS)类型“字符串”上不存在属性“名称”。
Error TS2339 (TS) Property 'productid' does not exist on type 'string'. 错误TS2339(TS)类型“字符串”上不存在属性“产品ID”。

Why product is a string instead of type IProduct ? 为什么product是字符串而不是IProduct类型?

for ... in iterates the property keys of an object. for ... in迭代对象的属性键 for ... of iterates the elements of an array. for ... of迭代数组的元素。 Use for ... of instead. for ... of代替。

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

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