简体   繁体   中英

Loop array returns element of type string instead of array's type

I have problems looping a TypeScript array. 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:

Error TS2339 (TS) Property 'name' does not exist on type 'string'.
Error TS2339 (TS) Property 'productid' does not exist on type 'string'.

Why product is a string instead of type IProduct ?

for ... in iterates the property keys of an object. for ... of iterates the elements of an array. Use for ... of instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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