Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to copy object without reference in angular

How to copy object without reference in angular ?

Aman Jain, April 13, 2022April 14, 2022

In this tutorial we will learn to copy object without reference in angular. Objects and array are work on reference by address methodology means Objects and array keeps the address means if you change anything in the object or array from anywhere then it will change the value in original object.

Angular is most popular frontend framework and not only in angular even in javascript or other framework we ca land into the same problem so to copy object without reference in angular here is the simplest solution

Copy object / Array in angular

Simplest way to copy a object in angular or javascript is below

let obj=[{"title":"test"}];
let copiedObj = JSON.parse(JSON.stringify(obj));

Here, we copied using json stringify and json parse method, now if you change value in copiedObj it won’t change the values of obj variable

Another way to copy the object we can use Object.assign but it won’t work in case of nested objects

let obj=[{"title":"test"}];
let copy = Object.assign({}, obj)

So as you can we have used two methods here

  1. Object.assign : Used to copy but nested objects not work
  2. JSON.parse and JSON.strigify : Used to copy the object without reference in nested object

Let’s take an example of convert JSON string to JSON object in angular

Step 1 : Create an angular app

First step is to setup an angular app using below command

ng new example-app

Step 2: Create a Component

Create a component and add json stringify and parse in component

ng g c hello-world
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-hello-world',
  templateUrl: './hello-world.component.html',
  styleUrls: ['./hello-world.component.css'],
})
export class HelloWorldComponent implements OnInit {
  obj:Array<object> = [{"title":"test"},{"title":"test2"}];
  copy: Array<object>;
  copyWithoutRef: Array<object>;
    
  constructor() {
    this.copy = JSON.parse(JSON.stringify(this.obj));
    this.copyWithoutRef=this.obj;
    this.copy[0]['title']="Copied Title";

  }

  ngOnInit() {}
}

Here we used to convet the string to object and object to string.

Step 3 : Create template and add html

Now, add simple html template with a *ngFor

<h2>Copy without reference</h2>

<div *ngFor="let val of copy">
  {{ val.title }}
</div>

<h2>Copy with reference</h2>

<div *ngFor="let val of copyWithoutRef">
  {{ val.title }}
</div>

Live Example and code:

Related

Javascript Angular angularCopyobject

Post navigation

Previous post
Next post

Related Posts

Softwares disable change detection anagular

How to disable automatic change detection strategy in angular component?

July 4, 2021November 8, 2023

In angular all components by default take the changes as any event occur, but sometime we disable automatic change detection strategy in angular. Thus, to detach the two way binding in component we can use changeDetection strategy in component metadata. Angular Change Detection Strategy is simple to implement using the…

Read More
Javascript Angular KeyDown Event on Input Example

Angular KeyDown Event on Input Example

September 22, 2022March 16, 2024

Angular KeyDown Event on Input is an Angular event binding it triggers when user interacts with DOM like text based input element and when we key down the input. Input events in angular can be handled using input and output decorators and in this tutorial we will learn Angular KeyDown…

Read More
Javascript localStorage.setItem(key,value)

How to store data in local storage in angular ?

March 28, 2022

While working in angular application we want to store data in local storage for example want to store user login data or some calculations. In this article i will show you to store objects, array, integer, string etc. in local storage. We will going to use native API for storing…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version