The loop is returning a string element instead of the expected type from the array

I am facing an issue with looping through a TypeScript array. The following methods are being used:

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 am unsure as to why the error

Option(product.name, product.productid)
is being thrown:

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

Why is product being recognized as a string instead of type IProduct?

Answer №1

When using for...in, you iterate over the property keys of an object. On the other hand, for...of is used to loop through the elements of an array. It is recommended to use for...of instead.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Newbie's guide to setting up babel for material-ui in Next.js!

Helpful Resources: Click here "For better bundle size optimization, create a .babelrc.js file in your project's root directory: const plugins = [ [ 'babel-plugin-transform-imports', { '@material-ui/core': { ...

The switch case functionality refuses to change when I interact with the user interface

Can someone please help me troubleshoot? I'm not receiving any errors in the Chrome console. HTML <div class="wrapper"> <i id="repeat" class="fas fa-stop-circle"></i> </div> Javascript const wrap ...

When scrolling, the selected text does not maintain its correct position

I'm looking to enable my users to search by selecting text. Once they select text, a new button will appear on the right side of the selected text giving them the option to search or not. However, when the user scrolls to the bottom of the page, the p ...

Hide the FormArray in a Reactive Form if it is not populated or cannot be determined

As a newcomer to Angular 4, I've embarked on my first project and started learning the ropes. I have devised a Reactive Form to showcase a form structure that looks like this: Form (FormGroup) | --> aggLevels (FormArray) | --> ...

What is the process for the event loop moving into the poll phase?

There is a scenario outlined in the event loop explanation on the Node.js official website. When setTimeout is triggered, and the callback queue for the timer phase isn't empty, why does the event loop move on to the poll phase? The site mentions that ...

What could be the reason for this code not waiting for module imports?

Currently, I am facing an issue with dynamically importing modules in a nodejs server running in the development environment. To achieve this, I have implemented an immediately-invoked async function which, in theory, should work perfectly. However, it see ...

Component's state not reflecting changes after dispatching actions in Redux, though the changes are visible in the Redux DevTools

When a menu item is clicked, I execute the following code: import React, { Component } from 'react'; import 'react-dropdown-tree-select/dist/styles.css'; import { connect } from 'react-redux'; import '../../../css/tree.c ...

What is causing the recurring failure of file input changes to take effect?

Here is the code snippet I'm working with: <input type="file" #fileInput ng2FileSelect [uploader]="uploader" (onFileSelected)="onFileSelected($event)" /> Along with the handler function: public onFileSelected(e: Fi ...

Define a new type in Typescript that is equal to another type, but with the added flexibility of having optional

I have 2 categories: category Main = { x: boolean; y: number; z: string } category MainOptions = { x?: boolean; y?: number; z?: string; } In this scenario, MainOptions is designed to include some, none, or all of the attributes that belong to ...

What is the process for the Rebol programming language to independently implement an asynchronous pluggable protocol for reading

This post outlines the process of incorporating an asynchronous pluggable protocol in Rebol that can be accessed through Firefox, Internet Explorer, or the command line For instance, if I were to define the reb:// protocol, I could enter it into a browser ...

Retrieving data from a database using a Symfony2 controller in JavaScript

I have been trying to retrieve a list of categories from my database and store it in my JavaScript code for future use. However, I have run into issues with this task as the list appears empty after being returned to JavaScript. Below is the code for my S ...

Thinking of hosting an event centered around Google Maps?

Are there specific event listeners for panning or zooming the Google Map, in addition to the mouseover, mouseout, and click events? UPDATE: I tried using 'center_changed', but it didn't work the way I expected. Whenever I move the mouse ov ...

Dynamically defined type literals in Typescript

Recently, I came across an intriguing problem. I am attempting to develop a Vue.js wizard component that takes in configuration objects. The type of demo I have looks like this: type WizardConfiguration = { steps: Array<{ name: string, fie ...

Make sure to clear the timeout in JavaScript before re-calling the function again

Scenario: Whenever a user clicks on a cell within the "#test" table, the "update_func" function will run and repeat every 10 seconds. If the user clicks on the same cell or another cell, multiple instances of "update_func" start running simultaneously ev ...

Encountering the error message 'array expected for services config' within my GitLab CI/CD pipeline

My goal is to set up a pipeline in GitLab for running WebdriverIO TypeScript and Cucumber framework tests. I am encountering an issue when trying to execute wdio.conf.ts in the pipeline, resulting in this error: GitLab pipeline error Below is a snippet of ...

Styling components using classes in Material-UI

I recently started using material-ui and noticed that it applies inline styles to each component. After running a test with multiple instances of the same component, I realized that there was no CSS-based styling - only repeated inline styles were generate ...

Press on the thumbnails in the gallery slider to activate links to external thumbnail images

I am looking to activate both the .main and .thumb links by clicking either one of them. //The following code activates both .main and .thumb when the .main link is clicked. $(".main a").on("click", function(){ var target= $(this).attr("href"); ...

No content found in jQuery .text() values within iframe

I am experiencing an unusual issue with assigning a value to a variable using jQuery. I am unable to retrieve the values from .text() specifically on Spotify, while other functions on different sites are working fine. Even after the elements have loaded, i ...

Sending a basic javascript variable to a PHP script

I need to send a basic JavaScript variable to another PHP file that serves as a controller within Laravel. For instance, the following code is from my blade.php file in Laravel: <script> function getLocation() { if (navigator.geolocat ...

JSON autocomplete feature for text input field

I've been struggling to implement an autocomplete textbox that utilizes a JSON file as its data source. Currently, the app is hosted on Google App Engine and the autocomplete function is based on an array of countries hardcoded into the code. However, ...