Parent Parameter Implementation in JS Reduce Function

I am currently working on enhancing my reduce function to transform a nested JSON object into a flat list for more convenient searching.

Consider the following JSON:

    {
  "MovementPatterns": [
    {
      "Id": "",
      "Name": "Warm-up",
      ...

Here is the code I have been using:

const exercises = data.MovementPatterns.reduce(
  (a, {Exercises}) => [...a, ...Exercises, ...a],
  [],
);

This code successfully flattens all exercises from each movement pattern into a single list. However, I now need to add the PARENT Movement Pattern ID to each exercise in the JSON.

[
        {
          "Id": "",
          "Name": "Lizard Stretch",
          ...

I would greatly appreciate any assistance in modifying my reduce function to achieve this additional requirement :)

Thank you!

Answer №1

You're getting very close! To complete the task, simply attach the parent's Id to each element in the Exercises array as MovementPatternId.

const updatedExercises = ab.MovementPatterns.reduce(
  (accumulator, { Exercises, Id }) => [
    ...accumulator,
    ...Exercises.map(exercise => ({ ...exercise, MovementPatternId: Id }))
  ],
  []
);

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 an array that was created using the useEffect hook in React?

Utilizing the power of useEffect, I am fetching data from two different APIs to build an array. My goal is to access this array outside of useEffect and utilize it in the return statement below to render points on a map. However, when trying to access it ...

conceal parent window element upon clicking an image within an iframe

I'm facing a challenge with hiding certain buttons in a parent window when opening a modal by clicking an image inside an iframe. Below is the code snippet that I am using: In the parent window - <iframe id="gallery" src="links/gallery.html" wid ...

Ways to trigger an onClick event of one div based on the presence of another div

I'm looking to execute a function when a specific type of button is clicked on my HTML page. I have approximately 10 buttons on the page and I've written the following code to call a function when a button is clicked: $('.divname').ea ...

Issues with Angular routing functionality being experienced

Currently, I am experimenting with Angular routing within my Ruby on Rails application. The issue arises when I navigate to a specific route, such as '/', and have an Angular routing setup to load a template from a designated directory. This tri ...

Are we on the correct path for breaking down code using react JS?

As I work on creating reusable table components, each column comes with its own set of functionalities, which results in dealing with numerous components. To streamline the process, I have been separating every component piece into individual files and ex ...

Displaying JSON data obtained from two separate arrays in AngularJS - is it possible?

I want to display the value of my Json element, "Baru20151","Lama20151","Baru20152","Lama20152", but I'm unsure how to do it. The element is fetched from 2 arrays, for the "Baru20151" elements, "20151" is obtained from the "isimasa" array in my Jso ...

When I change the camera, the camera control becomes active

I needed to switch the camera type in my three.js Demo, so I referred to an official demo at: However, I encountered some errors when testing my demo. Pressing 'P' to use the cameraPerspective worked fine, but when I tried to switch to cameraOrt ...

What is the most efficient method for utilizing the dist and public directories in a webpack application?

When working on a webpack project, I've noticed that most tutorials online suggest using the same folder for assets and distribution. However, I am interested in exploring different approaches to managing assets and static files before compiling every ...

A guide on how to showcase an alert message box with a radio button using JavaScript

<html> <body> <button id="popup-btn">Click for Popup</button> </body> <script type="text/javascript"> var popUpContent = $('<div><input type="radio">Option A<br>< ...

What is the Ideal Location for Storing the JSON file within an Angular Project?

I am trying to access the JSON file I have created, but encountering an issue with the source code that is reading the JSON file located in the node_modules directory. Despite placing the JSON file in a shared directory (at the same level as src), I keep r ...

Using Vue.js, is it possible to apply a class to a clicked element and then remove it when another element is clicked?

While developing an app using Vue.js, I have run into an issue. I am familiar with how to solve this problem using jQuery, but I am struggling to find a solution within my Vue app. Let me explain quickly - I have a list of items and I want to assign a spec ...

Discover the mean number of events

I have a database with a variety of events, where each event is stored in the db.events collection like this: { "_id" : ObjectId("5e56c7d0c0c979b198cbe21a"), "event" : "buy", "userId" : "u1", "itemId" : "iPhone 12", } Since the collection ...

Using the same geometric shapes repeatedly leads to occasional texture errors in THREE.JS

As I embark on my journey to create my first card game using THREE.js for Firefox and Chrome, I have encountered a perplexing issue with disappearing textures. The purpose of this post is to gain insights into why this issue occurs and how I can resolve i ...

Employ CSS flexbox and/or JavaScript for creating a customizable webpage

I am in the process of developing a basic IDE for an educational programming language that has similarities to Karel the Dog. One major issue I am encountering is creating the foundation HTML page. The IDE consists of 4 main areas: Toolbox (contains but ...

Anticipating the fulfillment of promises with the power of Promises.all

I have adopted the code found here for loading multiple .csv files. Upon successful loading of these files, my intention is to then render a react component. Below is the method located in datareader.js that I am currently working with. I am exploring the ...

What is the process for enabling a Submit button and updating its value following a successful Ajax response within a Django environment?

My current project involves using Django as the web framework. I am in the process of setting up an AWS lab or AWS account. To avoid multiple submissions, I have disabled the Submit button once the user enters information such as lab name, CIDR block, etc. ...

Utilizing CSS transitions in tandem with jQuery mousemove

Currently, I am developing a game that involves clicking and dragging using jQuery. Within the red game div, when you click an object appears that can be dragged around. To view a demonstration of this functionality, you can visit the following fiddle link ...

The attention is consistently drawn to the link within the OffCanvas element

I had successfully created a script that automatically gives focus to a particular element in a popup div when the down key is pressed in a textbox. However, things took a turn when I introduced a sidebar navigation pane using a Bootstrap offcanvas compone ...

Save JSON data in an HTML document or vice versa

Hey there, the title of this post might seem a bit unclear but I couldn't think of another way to phrase it. So here's the dilemma: I have some data that is JSON encoded and stored in a .json file: {"foo":"bar", "bar":"foo", "far":"boo"} Additi ...

Issue with specialized HTML block on WordPress

I've been working on a website and I added a custom HTML block with HTML, CSS, and JavaScript. However, when viewing the page, it seems to be hidden behind other content for some reason. Here's where the custom HTML is located This is how the ...