Tips for showcasing information from a nested array and modifying data in an Angular application

I am in the process of developing a cutting-edge Angular application to monitor my fitness workouts at the gym.

Enclosed within this array are the diverse exercise names, repetitions, sets, weights, and more that constitute each workout:

workoutExercises = [
    {
      id: 0,
      name: "Close Grip Bench ",
      setOne: [5, 18.5, false], // repetition, weight, setComplete
      setTwo: [3, 18.5, false],
      setThree: [2, 18.5, false],
      setFour: [8, 18.5, false],
      setFive: [8, 18.5, false],
      currentExercise: true,
    },
    {
      id: 1,
      name: "Barbell Rows",
      setOne: [5, 19, false],
      setTwo: [3, 28.5, false],
      setThree: [2, 38, false],
      setFour: [8, 47.5, false],
      setFive: [8, 47.5, false],
      currentExercise: false,
    },
    {
      id: 2,
      name: "Pull Ups",
      setOne: [6, 0, false],
      setTwo: [6, 0, false],
      currentExercise: false,
    },
  ];

My objective is to exhibit information from the initial set setOne: [5, 18.5, false] on the user interface.

Afterward, upon the user clicking Done, I intend to advance to the subsequent set setTwo: [3, 18.5, false], and display relevant details sequentially.

My initial thought was incorporating a boolean variable like currentExercise on the first set, which would update once the set is concluded. However, I believe there might exist more optimal solutions.

Could someone kindly suggest the most effective approach for addressing this particular challenge?

Please keep in mind that once an entire exercise has been completed, I aim to transition to the primary set of the next exercise.

Answer №1

To enhance your data model, consider organizing your exercise steps within an array named "steps":

const workoutExercises = [
  {
    id: 0,
    name: "Close Grip Bench ",
    sets: [
      [5, 18.5], // repitition, weight, setComplete
      [3, 18.5],
      [2, 18.5],
      [8, 18.5],
      [8, 18.5]
    ]
  },
  {
    id: 1,
    name: "Barbell Rows",
    sets: [
      [5, 19],
      [3, 28.5],
      [2, 38],
      [8, 47.5],
      [8, 47.5]
    ]
  },
  {
    id: 2,
    name: "Pull Ups",
    sets: [
      [6, 0],
      [6, 0]
    ]
  }
];

Subsequently, you can restructure your object to unify the sets into one array containing individual objects with their respective ids and names:

const workouts = workoutExercises.map(e => [...e.sets.map(val => {
  return {name: e.name, id: e.id, set: val}
}
)]).flat()
console.log(workouts)

Check out the console for the resulting output:
https://i.sstatic.net/Kh6CB.png For a live example, visit this link.

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

Navigating through Angular components can sometimes pose challenges in component routing

I have set up child component routing within my application. I am facing issues when trying to navigate to the default route using routerLink. Since the parent path includes ':id', the child routes only work when prefixed with ':id' ...

Deactivating PrimeNG checkbox

I am currently facing an issue with disabling a PrimeNG checkbox under certain conditions by setting the disabled property to true. However, whenever I click on the disabled checkbox, it refreshes the page and redirects me to the rootpage /#. To troublesh ...

PHP - changing MySQL information into a JSON array

I am currently working with codeigniter and I need to retrieve data from a database and convert it into a JSON object instead of a JSON array. The code I am using is as follows: public function json() { $content = $this->db->get('todolist& ...

What causes the appearance of the "?" symbol at the beginning of the URL and triggers a reboot of the app when moving through the absolute path?

I am facing an issue. In my multi-module application with lazy loading, I encountered a strange behavior when trying to navigate between child lazy modules. Transition from top module to bottom child module works fine, but routing from one bottom child la ...

Retrieving data from JSON arrays based on specific conditions in Java

I have been utilizing Unirest to retrieve all IDs from an endpoint. The array structure is as follows: [ { "date": "2022-04-05", "timeIn": "2022-04-05 07:00:00", "timeOut": " ...

Tips for identifying the cause of a memory leak in browser notifications

I am looking to implement browser notifications in a browser extension. However, I have noticed that the memory usage does not decrease after closing the notification. Can someone explain why this may be happening? Allow StackOverflow show notifications i ...

What is the best way to merge an array into a single object?

I have an array object structured like this. [ { "name": "name1", "type": "type1", "car": "car1", "speed": 1 }, { "name": &q ...

Storing values beyond the boundaries of an array in C++

char *variable = new char[5]; variable[10] = 'f'; How come this code does not throw an error when executed in debug mode? It leads to a crash in a release build. ...

A guide on integrating a data deletion feature in Angular applications

On our website's edit page, there is a list of mat cards/workspaces with an edit icon in the top corner of each workspace. Clicking on the edit icon will take you to the edit page for that specific workspace. Within this edit page, there is a delete b ...

Can you tell me the equivalent in Angular of the hasClass method?

Looking to target elements with a designated class and dynamically update their contents ...

Executing Karma tests in IntelliJ with Angular 6

After upgrading my angular application from version 5.2 to version 6, everything seems to be working smoothly. However, I encountered an error while trying to run a test from IntelliJ: Error: The '@angular-devkit/build-angular/plugins/karma' ka ...

The formgroup label is currently displayed in uppercase, but I would prefer it to be in title case

In the angular template below, I have noticed that even though the labels are in titlecase, they appear in uppercase when the page is rendered. This wasn't the desired outcome so I attempted to use the titlecase pipe and enclose the label text within ...

What to do when the 'image' property in Next.js next/image has an implicit 'any' type and needs fixing?

I'm a newcomer to Next.js and TypeScript and I can't seem to find any helpful resources on resolving this particular issue: import Image from 'next/image'; export default function Item({ image }) { // <-- parameter image needs a &ap ...

What steps can be taken to display database results solely for the user currently logged in and created by them?

Currently, I'm in the midst of a project that involves extracting an HTML list from JSON data using JavaScript. The list is being displayed on the logged-in user's profile, showcasing job listings from the JSON data. While I've successfully ...

The bar chart functions perfectly on localhost but encounters issues after being hosted on Gitpage

Localhost Gitpage The bar chart was displaying correctly on localhost, but once it was hosted on Gitpage, it began to show issues. Any suggestions on how to resolve this? Repository Link: https://github.com/mzs21/bar-chart Live Preview: ...

Combining arrays of objects sharing a common key yet varying in structure

Currently, I am facing a challenge while working on this problem using Typescript. It has been quite some time since I started working on it and I am hoping that the helpful community at StackOverflow could provide assistance :) The scenario involves two ...

Uploading Images to Firebase Using Ionic 2 Gallery Selection

Check out the code snippet below for capturing images using the Ionic/Cordova Camera Plugin. There are two functions defined: one for capturing an image from the camera and another for uploading an image from the gallery. capture() { const cameraOptions: ...

initiate an animated sequence upon the initialization of the Angular server

Is there a way to launch a Netflix animation after my server has started without success using setTimeout? I don't want to share the lengthy HTML and CSS code. You can view the code for the animation in question by visiting: https://codepen.io/claudi ...

ReplaySubject in Angular is failing to update the array when a new object is added

I am encountering an issue where, upon attempting to create a new page Object, it successfully sends the data to the backend but does not update the array. I have to refresh the page in order to view the entire array. Within the frontend, I am utilizing O ...

Navigational bar mishaps: troubleshooting the "is not a function error" in layout.tsx

I recently started learning React (Next.js) and I'm working on adding a navbar feature to my project. The goal is to have the active navlink stand out when the user is on the corresponding page. To achieve this, I created a function that updates the s ...