Troubleshooting problems encountered when duplicating an array in JavaScript

I am attempting to utilize properties and flatmap to modify an array without altering the original data. I have implemented this method in two different instances within a single dispatch call in Vue.js when transferring data from parent to children components.

  • Initially, everything works fine when using the method for the first time. However, when accessing the array for the second time in a different child component, it appears that changes were made to the original data unintentionally. I aim to prevent any modifications to the original array (JS, Vue.js, & TS).
public mapJson(data:any){
            let copyData:any = [...data]

            const newObjects:Array<MetaObject> = [];
            const cb = (e) => {
                let element: any = {...e};

                if(element.item.parentId.value === 0){
                    element.item.parentId.value = null;
                }
      
                if(element.item.idOnScene){
                    let split = element.item.idOnScene.split("|");
                    element.item.modelId = element.item.id.value;
                    element.item.id.value = split[2];
                    element.item.name = split[1];
                    element.item.type = split[0];
                }
                else{
                    element.item.modelId = element.item.id.value;
                }

                let newMeta = {
                    id: element.item.id.value,
                    name: element.item.name,
                    type: element.item.name,
                    parent: element.item.parentId.value,
                    modelId: element.item.modelId
                }
                newObjects.push(newMeta);

                e.children && e.children.forEach(cb);
            }
            copyData.forEach(cb);

            const newData : Metadata = {
                id: "",
                projectId: "2GcXDGLMXA_8_$wW69Hy7E",
                author: "",
                createdAt: "2019-01-21T13:24:08",
                schema: "IFC2X3",
                creatingApplication: "20181011_1500(x64) - Exporter 18.4.0.0 - Alternate UI 18.4.0.0",
                metaObjects: newObjects
            };

            return newData;
        }

Answer №1

Utilizing JSON.parse(JSON.stringify(object)) successfully resolved the problem

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

VueJS error: 'Property or method is not defined on the instance' message appears despite the method being defined on the instance

Is there a way in Vue.js to dynamically assign the class 'adjust' based on the value of a computed property called "isTrueSet"? I keep encountering the following error message: [Vue warn]: Property or method "itTrueSet" is not defined on the inst ...

The creation and addition of Vue components dynamically has failed due to the inability to mount the component, as the template or render

After going through numerous Stackoverflow posts with similar symptoms, I found that none of the solutions solved my issue. According to this particular post, there are various reasons that could cause this problem. To troubleshoot, I upgraded all my npm, ...

The URIError occurred while attempting to decode the parameter '/December%2015,%' within the express framework

After setting up a middleware using the express framework that handles a URI with a date as a parameter, I encountered a small issue. app.get("/:date",function(req,res){ var result; var myDate=req.params.date if(moment(myDate).isValid()) ...

Struggling with implementing Mobile Navigation menu

I'm struggling to implement a mobile menu for my website. After adding the necessary code, when I view the page in a mobile viewport, all I see are two sets of periods ("..."). However, unlike in the code snippet provided, the list does appear on the ...

Can the keys be extracted from the combination of multiple objects?

Basic Example Consider this scenario type Bar = { x: number } | { y: string } | { z: boolean }; Can we achieve type KeysOfBar = 'x' | 'y' | 'z'; I attempted this without success type Attempted = keyof Bar; // ...

Using jQuery to select a nested element in HTML

After choosing $("a.tp[programm='" + programm + "']"); I am looking to target the nested element span.thump and update its text content. How can I achieve this? <h4><a programm="74" class="tp" href="#"><img src="/images/tuo.png ...

Is there a way to pre-load the data prior to the component rendering in ReactJS?

The main goal of my project is to retrieve data from the Google Analytics API and display it as a list. Although I am able to successfully fetch the data from the API, I am encountering an issue when passing it to another component. While I can view the da ...

What is the best way to extract specific information from an API using AJAX and Axios?

I can't figure out what I'm doing wrong when trying to access a URL from an API using Axios. For instance, I'm attempting to select the following URL: "https://media4.giphy.com/media/yy6hXyy2DsM5W/giphy-downsized.gif?cid=482277c2s3j1s8uell6v ...

What is the best way to exclude an external HTML file from being displayed on the home page?

shopping_cart.php <div id="main-container"> <div class="container"> <span class="top-label"> <span class="label-txt">Shopping Cart</span> </span> <div class="content-area"> <div class="con ...

Guide on Applying a Dynamic Color in VueJs 3 Composition API/Vuetify Using CSS

Currently, my project utilizes Vue 3 with the composition API and Vuetify for the UI. I am looking to utilize a color that is already defined in a Vuetify theme variable within my CSS, similar to how I have done it previously in JavaScript. Although I at ...

Delete the item if the link uses Javascript: void(0)

<a class="slicknav_item" href="home">Home<span></span></a> <a class="slicknav_item" href="about">About<span></span></a> <a class="slicknav_item" href="javascript: void(0)">Services<span></span& ...

Ways to verify whether a checkbox is selected and store the status in the LocalStorage

Hey there, I'm still new to the world of programming and currently just a junior developer. I have a list of checkboxes and I want to save any unchecked checkbox to my local storage when it's unselected. Below is a snippet of my code but I feel l ...

removing functionality across various inputs

I have a JavaScript function that deletes the last digit in an input field. It works fine with one input, but not with another. It only erases the digit in the first input. <script> function deleteDigit(){ var inputString=docu ...

Get a webpage that generates a POST parameter through JavaScript and save it onto your device

After extensive research, I have hit a roadblock and desperately need help. My task involves downloading an HTML page by filling out a form with various data and submitting it to save the responses. Using Firebug, I can see that my data is sent over POST, ...

Transferring the date from an HTML input to a TypeScript file in Angular when the button

On my HTML page, I have two input fields for dates that need to be passed to the backend along with other parameters when a button is clicked. Here is the current setup: <input [owlDateTime]="dt1" placeholder="From a date" [value]="dateFrom.value"& ...

What is the best way to bring in this interface from the React-Particles-JS library?

I have been attempting to segregate my parameters from my JSX within the react-particles-js library. I organize my parameters in an Object: let config = { "particles": { "number": { "value": 50 }, ...

You cannot invoke this expression while destructuring an array of React hooks in TypeScript

Within my React TypeScript component, I have several fields that check a specific condition. If the condition is not met, the corresponding field error is set to true in order to be reflected in the component's DOM and prevent submission. However, whe ...

"Secure access with Keycloak authentication in a .NET Core API and Vue.js Single Page Application

Currently, I am utilizing .NET Core for the backend REST API and Vue.js for the frontend. In my current setup, I have implemented authentication through a cookie. However, I am now looking to switch to authenticating via Keycloak. The goal is to have the ...

Child component destruction triggers an ongoing digestion process

I am facing a strange issue within my AngularJS application. The error I'm encountering is the common $digest already in process error. Despite searching online and reviewing similar questions, none of the proposed solutions have resolved the issue. T ...

Converting a timestamp from PHP in JSON format to date and time using JavaScript

Within the JSON file, there is a timestamp associated with each user login. An example of this timestamp is: timestamp: "1541404800" What steps should be taken to convert this timestamp into date and time format? ...