Incorporating an object into an array within another array using Angular 6 and Typescript

I am looking to insert an Object into an array that already contains another array by inputting the ID into a textfield. Can someone guide me on utilizing methods with arrays within arrays?

Check out the demo here:

Below are the classes for reference:

 class Food {
    id: number;
    name: string;
    price: number;
    type: string;
}

class Foodplan {
    id: number;
    foodPerWeek: number[] = new Array(5);

Any help would be greatly appreciated. Thank you!

Answer №1

Upon reviewing the stackblitz project, it appears that the foodPlan array contains only one element at index 0. To insert additional food items into this element, you can use the following code snippet:

this.foodPlan[0].weeklyFood.push(id);

For more details and an updated version of the stackblitz, please visit this link. If you plan to enhance the functionality to support multiple elements in the foodPlan array, remember to identify the specific index for adding new food items.

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

What is the best way to retrieve all classes that have a hyphen/dash in them from a DOM element and then store them in an array using JavaScript?

How can I retrieve all classes that contain a hyphen/dash applied to any DOM element and store them in an array using JavaScript? My current approach: const getClasses = []; const classesContain = []; document.querySelectorAll('*').forEach( ...

``The positioning of the element does not seem to be effective with the selected [

I'm struggling to position a div using [style]. Even after following the advice from various sources, I can't seem to place the div correctly at 100,100. Could someone please review the code snippet below and point out any errors? Check out the ...

Creating a PDF file from JSON data and enabling file download in a web application with React and JavaScript

Could you provide guidance on how to convert json data into a pdf file without using any library? Here's an example of the json data I'd like to convert; {"employees":[ {"firstName":"John", "lastName":& ...

Creating a polygon with Konva js by connecting lines: a step-by-step guide

I am seeking a way to empower the user to sketch out a polygon using lines, creating the desired shape and then apply transformations to it (such as rotation and resizing). What is the best method for implementing this functionality with Konvajs? https:/ ...

The Angular 18 component was brought in as a dependency and integrated into the project, but unfortunately, it

How do I properly integrate the NavbarComponent into the template of the app component? https://i.sstatic.net/pBKTzUnf.png app.component.html <app-navbar></app-navbar> <router-outlet></router-outlet> main.ts import {CdkTableModul ...

Exploring the Possibilities of Nipplejs Integration in Vue with Quasar

Trying to implement Nipplejs in my Vue Project using quasar Components. Installed nipplejs through npm install nipplejs --save. Attempted integration of the nipple with the code snippet below: <template> <div id="joystick_zone">&l ...

Using Node-Forge for importing in an Angular 2 service

I've been attempting to incorporate Forge (https://github.com/digitalbazaar/forge) into my Angular 2 project. After executing the command :npm install node-forge, the node-forge directory was created within my application (in the node-modules directo ...

The Monaco editor has the ability to identify capitalized functions as classes

My question pertains to using the Monaco Editor with custom TypeScript code. The illustration shown below is intended to highlight the issue: var Scheduler = { configModel: function (): SchedulerConfig { return null; }, ConfigModelCap: ...

Add an <input> element to the innerHTML in Angular 2 using injection

My attempt to insert an input HTML tag using Angular 2 has hit a roadblock. Check out my project below: <div [innerHTML]="inputpdf"></div> Looking at the .ts file: export class FaxSendComponent { inputpdf = '<input type="text" ...

What causes the InvalidValueError to appear in the 'origin' property when using Angular? Is it necessary to set either a location, placeId, or query to

I have integrated agm core into my angular10 application to display google maps. The code in component.html is as follows: <agm-map [latitude]="latitude" [longitude]="longitude"> <agm-direction *ngIf="source && ...

Using a function as a prop in React Native can result in an error, specifically when trying to call an 'onPress' function which is not recognized as a valid function and is instead seen as

Hey there! I'm facing an issue while trying to trigger an action in the parent component from a child component using props. One of the props in the child is a function that should update the parent's state when a button is pressed, triggering a ...

Error! Element not found in cloned iframe #2460, promise unhandled

Can you help me troubleshoot this issue? This is the code I'm working with: const section = document.createElement("section"); const myHTMLCode = "<p>Greetings</p>"; section.insertAdjacentHTML("afterbegin", my ...

Does the onAuthStateChanged listener in firebase trigger immediately upon user login or logout?

//initialize auth change listener useEffect(() => { auth.onAuthStateChanged((user) => { if (user) { router.replace('/') } }) setInitializing(false) }, []) //*handled by login button const login = asy ...

I am receiving a warning about Tailwind CSS in my project and I'm unsure how to proceed

After moving on from a live project built with Angular, I revisited the project only to encounter a warning related to Tailwind CSS in my styles. It seems like there is an issue with importing statements conflicting with other rules. Despite attempting var ...

Is it possible to retrieve the state or access the store within effects?

When attempting to access the current state/store in NgrX effect, only the initial state is returned. However, accessing it from the component works perfectly fine. Effects class @Injectable() export class RootEffects { /* To handle the behavior ...

Is there a way to stop PrimeNG Tree onNodeSelect() event from triggering when utilizing templating?

How can I prevent a PrimeNG Tree with templating from selecting/deselecting the node on any click inside the template? Specifically, I want only a click on the <a> element to call doSomething(), not nodeSelected() as well. Here is the code snippet: ...

Incorporate a Google map into a table row by extending it across all table data cells

Apologies for the vague title, I am having trouble articulating my issue. Nevertheless, I am dealing with a complex HTML table structure as displayed below: <div *ngFor="let item of result; let i = index"> <table ...

Angular encountering issues with loading external JavaScript files due to the error: ENOENT - indicating that the specified file or directory does

I am attempting to incorporate a bootstrap template into Angular. The template requires some external JavaScript and CSS files that need to be linked. I have placed these files in the assets folder and referenced them in the styles and scripts arrays of an ...

Creating an array and initializing it within a Java constructor

I am trying to initialize a two-dimensional array in the constructor of my class. However, I have encountered an issue when declaring the instance variable of the array within the class. When I try to declare it in this way: public class Data { private St ...

Encountering an issue in Angular 2: CKEDITOR Error code editor-destroy-iframe

I have integrated ng2-ckeditor into my Angular2 project and everything is working smoothly. However, I am encountering a warning message [CKEDITOR] Error code: editor-destroy-iframe when navigating to another component. After researching for a solution, ...