Setting the values of a string array to the properties of an object dynamically in TypeScript

Imagine a situation where a component called searchBox is given two inputs: @Input() titles: string[] and @Input() model: Object. The number of values in the titles array matches the number of properties in the model object. The searchBox component generates an input box for each item in the titles array, allowing users to enter search terms which are then stored in a string array named titlesValues. In order to properly link these values to the corresponding properties in the model object, the searchBox component needs to assign the values of titlesValues to the model properties one by one and output the updated model using @Output resultModel: Object. I attempted to dynamically access each property of the model object for assignment as shown in the code snippet below:

let i =0;
  Object.keys(this.model).forEach((key) => {
     this.model[key] = this.titlesValues[i];
   });

Even though I've tested multiple statements and alternative approaches to achieve the desired outcome, I encountered the following error:

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Object'.

How should I go about implementing this scenario effectively? Thank you.

Answer №1

To fix the issue, there are two options available. The first option is to explicitly specify that model should be of type any:

@Input() model: any;

Alternatively

You can create a new interface for the model and use it like this:

interface Model {
    [index: string]: string;
}

@Input() model: Model;

Answer №2

Let's attempt to modify the following:

@Input() headers: any[]

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

Maintain the same UI appearance when the checkbox state is altered

<input type="checkbox" [(ngModel)]="rowData.is_permitted" (change)="changeStatus()"> changeStatus() { rowData.is_permitted = true; } When I uncheck the checkbox but conditionally want to select it again, the flag is updated but does not affect the ...

Proper usage of a method within another method in a Vue component

Currently, I am extracting GPS data from an image using exifjs. My objective is to convert the latitude and longitude variables into a decimal variable as illustrated below: <template lang="html"> <div class="upload-wrap"> <button cla ...

Unable to display Md-checkbox component in Angular 2

I have been attempting to display a checkbox within my Angular 2 application, however, it doesn't seem to be appearing. What are some potential reasons for this issue? <md-checkbox [checked]="true">Unchecked</md-checkbox> <md-checkbox ...

IE8 triggers automatic download upon receiving JSON response using jQuery

Can you help me make IE8 compatible with this code using jQuery to handle an ajax request that returns data as JSON? $.ajax({ url: formAction, type: 'post', dataType: 'json', data: form, success: ...

Is there a way to turn off step navigation in bootstrap?

Displayed below is a visual representation of the bootstrap step navigation component. Presently, there is an unseen 'next' button located at the bottom of the page. When this 'next' button is pressed, it transitions from 'step-1 ...

The Electron React app crashes due to difficulty parsing the source map

I'm a beginner in the coding world and currently working on creating an electron app using react. One of the functionalities I want to implement in the app is the ability to save user login information so that the data can be automatically fetched whe ...

Adding a class to the body for a specific route in web development

I'm facing a situation where there is a class named: product-page-bottom-padding The requirement is to apply this class only to the /product/{slug} route for the body element. It should not be present in any other routes. Can you suggest how to mana ...

The hyperlink function is not operational in Gmail attachments

Using an anchor tag to navigate to a specific section within the same page works perfectly when the HTML file is on my local machine. However, when I attach the file in Gmail and open the attachment, it doesn't work. Why is this happening? How can I m ...

The Jquery click event refuses to trigger

Below is the JavaScript code that is not functioning: $('#change-priority-modal').find('.btn-primary').unbind().on("click", function () { //my_func_body } Interestingly, the code works perfectly fine in the developer console. I would ...

Aligning container divs at the center in various screen resolutions

I recently constructed a portfolio website using Bootstrap and Isotope JS. In my layout, I have a container div which works perfectly when viewed on a desktop browser - displaying 3 divs in one line. However, the issue arises when the resolution changes an ...

Tips for resolving the "trim" of undefined property error in Node.js

Looking to set up a basic WebAPI using Firebase cloud functions with express and TypeScript. Here's the code I have so far: import * as functions from 'firebase-functions'; import * as express from 'express'; const app = express( ...

Leverage the variable from one function in a different function within Three.js using Javascript

After loading an obj file using three.js, I attempted to obtain the 'X' position of its vertices and save it in a variable named 'pos' inside the objloader function within the init() function. My goal was to access this variable's ...

Strategies for managing outdated Angular reactive forms validation techniques

Consider a situation where I have an angular reactive form with an async validator that checks for the availability of a username. Initially, the username is not taken and is considered valid. However, upon submitting the form, the backend detects that t ...

Checking Sudoku Solutions on Codewars

I have come across this JavaScript code which seems to be functioning correctly. However, I am curious about the line board[3][8] != board[8][3] and how it checks for repeating row and column numbers. Can someone please provide an explanation? Thank you! ...

Tips on aligning a button next to an image

I need help adjusting my code to display a button next to an image on my webpage. I want it to look like an image button in a loop where users can download, but I am struggling with CSS. Can anyone please modify my code to meet my requirements? Thank you i ...

Using Firestore queries in your NodeJS application

Here's a query that is functioning as intended: const queryRef = firestore.collection('playlists').where('machines', 'array-contains', id) const snapshot = await queryRef.get() ... const playlist = document.data() as Pl ...

Sending Emails Using Javascript & CSS via SMTP in a Contact Form

Currently fine-tuning a contact form and nearing completion. However, there seems to be a minor error preventing the form from submitting correctly. Expected behavior includes a pop-up confirmation box upon submission and successful message delivery. Yet, ...

Including the Advanced Custom Fields (ACF) PHP value into a JavaScript array within a foreach loop

I am currently working with a code snippet that involves the variable $v. Each $v holds a specific string value (ex: icon1, icon2, icon3, icon4): <script type="text/javascript"> var vArr = new array(); </script> ...

Making an Ajax Request to Retrieve Data from Multiple URLs

Currently, I am attempting to perform an ajax call using the following URL: . The page number in the URL needs to be dynamic based on the response received from this ajax call. Here is my current code snippet: $.when( $.get("http://exampleurl ...

Changing query parameters with Angular router navigate does not result in the page being loaded

I'm working on a component that utilizes the results of an API query. My goal is to enhance the user experience by adding a dropdown box for filtering purposes. However, I'm facing a challenge in getting the page to reload when the URL query para ...