Search code examples
angularangular2-services

How to use same model name in different Json array in Angular 2/4


I have two Json arrays like given below.

Items: any[]; Item: any = {};

Both array having same property name ItemName; Items array contains list of item details. If I select one item from Items array and copy to Item. When I update the value from Item it reflects in Items also. How to block this in Angular 2/4


Solution

  • You need to extract and make a deep clone of your selected item and manipulate it instead of the item in the array. You can do it like this:

    Items: any[]; // Your original array
    Item: any = {}; // One of the items in the previous array
    
    selectedItem: any = Object.assign({}, Item); // use this item instead of the previous one.
    

    Hope it helps! :)