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

Javascript Angular Click Event on Input Example

Angular Click Event on Input Example

September 22, 2022March 16, 2024

Angular Click Event on Input is an Angular event binding it triggers when user click or tap any DOM element and it fires a event of (click). Input events in angular can be handled using input and output decorators and in this tutorial we will learn Angular Click Event on…

Read More

jQuery redirect after Ajax success Laravel PHP

December 4, 2021December 4, 2021

In this article i will show you to redirect a page after successful response from Ajax request. Laravel or PHP provides itself to redirect a page using its own methods but while working with JavaScript Ajax Laravel methods are not useful because it won’t redirect the page. so in this…

Read More
Javascript get radio input value in jQuery

How to get radio input value in jQuery ?

October 31, 2021November 5, 2023

While submitting a form or validating a form using the jQuery, we can get all the values of form easily but radio button works differently and we need some extra efforts to fetch the value of radio input. So in this article i will demonstrate to fetch the value of…

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

  • July 2025
  • 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

  • Mastering Schedule Management with Laravel Zap
  • 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
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version