Create a full duplication of every field and array within an object using AngularJs with the deepCopy method

I am looking to replicate certain fields within an object in other fields of the same object, similar to the concept demonstrated below:

var customers = {
    apple: {
        papa: {
            en: "cool" 
        }
    },
    oranges: {
        papa: {
            en: "cool" 
        }
    } 
};

function deepCopyEn(src) {


    if (src.hasOwnProperty("en")) {
      src.fr = src.en;
      src.es = src.en;
    }
    else {
        if (src.constructor === Array) {
            for (var i = 0; i < src.length; i++) {
                deepCopyEn(src[i]);
            }
        }
        else {
            for (var prop in src) {
                if(src.hasOwnProperty(prop)) {
                    deepCopyEn(src[prop]);
                }
            }
        }
    }
}

deepCopyEn(customers);

console.log(customers);

However, I encountered an issue when attempting to apply the same concept to a class with an array and another field. The function did not work, resulting in the error: RangeError: Maximum call stack size exceeded. For reference, you can view an example here. Can someone assist me in updating my function to resolve this issue?

Answer №1

To ensure accuracy for scalar types ...

function cloneObject(src) {


    if((/string|number|boolean/).test(typeof src)) {
        return;
    }

    if (src.hasOwnProperty("en")) {
    ....

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

Transforming a massive JSON object into a Blob concerns directly converting it into an ArrayBuffer or Blob to prevent exceeding the maximum string length error

Situation: Within my application, I am encountering the following code: let blob = new Blob([JSON.stringify(json)], {type: "application/json"}); This code sometimes fails because the maximum string length allowed in Chrome is approximately 500M ...

Able to display the value when printing it out, however when trying to set it in setState, it becomes

Within my form, there's a function that receives the value: _onChange(ev, option) { console.log(option.key) // Value of option key is 3 this.setState({ dropdownValue:option.key }) // Attempting to set state with 'undefined' as ...

Modifying table background color using AJAX and jQuery?

Scenario: My web page is designed to automatically search for a specific cell input by the user. If the cell has been input with a value, the table's background color will turn red; otherwise, it will remain green. Currently, the table has not been p ...

Different ways to enhance max-http-header-size in Vue application

After being redirected from another application, I am unable to open the page and receive an error in the console: Failed to load resource: the server responded with a status of 431 (Request Header Fields Too Large). I came across information about max-h ...

Prevent Duplicate Service Instances in Angular

After doing some thorough research online, I've identified the root of my issue: multiple instances of a particular service are being created. I need assistance in pinpointing and rectifying this problem within my code. The secondary service is depen ...

NextJS Router delays data reloading until page receives focus

Struggling with creating an indexing page in NextJS. Attempting to retrieve the page number using the router: let router = useRouter() let page = isNaN(router.query.page) ? 1 : parseInt(router.query.page); This code is part of a React Query function withi ...

The PHP random number generator appears to be malfunctioning when being compared to the $_POST[] variable

In this section, random numbers are generated and converted to strings. These string values are then used in the HTML. $num1 = mt_rand(1, 9); $num2 = mt_rand(1, 9); $sum = $num1 + $num2; $str1 = (string) $num1; $str2 = (string) $num2; The following code ...

Efficiently sanitizing a JavaScript object using the replace() method in JavaScript

I have a data object structured like this {"paymethod_id":1,"business_id":76,"delivery_type":"1","driver_tip":0,"delivery_zone_id":6569,"delivery_datetime":null,"location":{&qu ...

If the variable is defined within $scope, ng-model-options will not be taken into account

In my current project, I am utilizing the AngularJS Bootstrap Datepicker (also known as uib-datepicker). My goal is to set a specific UTC offset on it, which can be achieved by setting the ngModelOptions like this: ng-model-options="{timezone: '+02:00 ...

Issue TS8011 in Angular 6 is related to the restriction on using type arguments only in files with the .ts extension

I have a project in Angular 6 where I need to integrate a JS library. This library is confidential, so I can't disclose its details. The problem I'm facing is that the TypeScript compiler seems to misinterpret characters like <<24>>, ...

Filter dates within a range using two input fields in AngularJS

I am trying to use two input fields to display data within a specific range of dates using the AngularJS date filter. (Range filter for AngularJS dates using two input fields) Check out my code below: var app = angular.module('app',[]).control ...

Can you stop a JavaScript event using another event?

Can you prevent a JavaScript event in the queue from being executed by another event? Situation: I have two ASP.Net controls, Age - TextBox Save - Button There are two JavaScript validation functions, ValidateAge() - verifies if the age is between ...

Issue with Bottle.py: When using AJAX, request.forms.get() is returning NoneType

Having trouble sending JavaScript data to a bottle.py server using AJAX? Despite trying numerous solutions from various sources, none seem to be working. To provide clarity, I'm focusing on the AJAX call code here. Can someone explain why request.for ...

Learn how to send multiple checkbox values using jQuery and AJAX requests

When trying to extract the value from multiple checkboxes, I utilize this particular code snippet: <form class="myform" method="post" action=""> <input type="checkbox" class="checkbox" value="11" /><br> <input type="ch ...

Transferring information between components within Angular.js (not Angular) applications

In my Product.js controller, I attempted to pass data from ManualPrescriptionLeft.tpl.liquid to ManualPrescriptionRight.tpl.liquid in Angular.js, not angular. However, whenever I try to do this, the component becomes unresponsive and I cannot locate the $c ...

Angular.js reports that the custom HTTP response header is missing

Despite Chrome showing the correct POST response headers, my custom HTTP header X-Auth-Token is returning null in the callback function for the POST request. Angular.js seems to only be returning Cache-Control and Content-Type, with everything else showing ...

Dealing with Regular Expressions in Javascript and PHP Challenge

I am struggling to achieve the same outcome with my JavaScript as I do with my PHP code. The issue lies in how JavaScript omits backslashes, unlike PHP. To address this, I have included random forward and backward slashes to cater for different systems. Se ...

The error message "email() is not a valid function when using the onclick attribute

Can anyone lend a hand? I feel like I must be overlooking something really obvious. I'm having trouble calling my function to execute my ajax call. Any assistance would be greatly appreciated. Thank you! Here is an excerpt of the HTML code: $(docu ...

Is it acceptable for a video to autoplay even if it is not connected to the DOM?

Consider the following three scenarios: document.adoptNode, document.importNode, and document.createElement with assigned properties. In all cases, the video autoplay feature is activated even when it's not connected to the DOM. This behavior diffe ...

The error message "TypeError: addNewUser is not a function in React.js onSubmit

What could be causing the error message "TypeError: addNewUser is not a function"? The issue arises when I complete the form and click save, displaying the error that addNewUser is not defined as a function. The problem occurs within the following code ...