Array not working properly when removing data in angular 6











up vote
0
down vote

favorite












I have a grid view and loop data from an array called objEntityType: EntityTypeVM; also I have created another an array equal to objEntityType as original dataset objEntityTypeOriginal: EntityTypeVM;



on search method, I'm filtering data on objEntityTypeOriginal and assign it to objEntityType.



But the problem is whatever I do to objEntityType same thins happen to objEntityTypeOriginal.



about my code below, In OnSearch() I need to search text so before that I need to empty the objEntityType But after I empty the objEntityType objEntityTypeOriginal also will empty.






import { Component, OnInit, Inject } from '@angular/core';
import { EntityTypeVM, EntityType, Entity, ApiResponseModel } from '../../../shared/models';


@Component({
selector: 'app-entity-create',
templateUrl: './entity-create.component.html',
styleUrls: ['./entity-create.component.scss']
})

export class EntityCreateComponent implements OnInit {
private objEntityTypeOriginal: EntityTypeVM;
private objEntityType: EntityTypeVM;

constructor() { }

ngOnInit() {

this.searchText = '';
this.objEntityType = new EntityTypeVM();
this.objEntityType.entities = [{ value: '001', synonyms: ['home', 'house']}];//this will replace with API data
this.objEntityTypeOriginal = = this.objEntityType;
}


EnterEntitySynonymsHandler(event: KeyboardEvent, index: number, synonym: string) {
if (event.key === 'Enter' && synonym) {
this.objEntityType.entities[index].synonyms.push(synonym);
this.objEntityType.entities[index].newSynonym = '';
this.objEntityTypeOriginal = = this.objEntityType;
}
}

RemoveSynonymItem(entityIndex, synonymIndex) {
this.objEntityType.entities[entityIndex].synonyms.splice(synonymIndex, 1);
this.objEntityTypeOriginal = = this.objEntityType;
}

// ----- problem In below method -----------------------
OnSearch() {
const searchText = this.searchText.toLowerCase();

if (searchText === '') {
this.objEntityType = this.objEntityTypeOriginal;
} else {

this.objEntityType.entities = ;//After this line .objEntityTypeOriginal.entities also will be empty.
this.objEntityTypeOriginal.entities.forEach(element => {
const itemAsString =JSON.stringify(element).toLowerCase();
if (itemAsString.indexOf(searchText) !== -1) {
this.objEntityType.entities.push(element);
}
});
}
}

}












share|improve this question






















  • Assuming the actual code is this.objEntityTypeOriginal = this.objEntityType;, this doesn't create a copy of the object. It assigns the object referenced by this.objEntityType to this.objEntityTypeOriginal. Those are thus two references to the same object. So when you modify the object referenced by on of the variables, the object referenced by the other is also modified: they both reference the exact same object.
    – JB Nizet
    Nov 11 at 7:40










  • Yes. whatever I do to one object same thing happens to other object. What is the solution for that..?
    – Isanka Thalagala
    Nov 11 at 8:03






  • 1




    Create a copy of the object (end its entities array) instead of just assigning it to another variable.
    – JB Nizet
    Nov 11 at 8:06










  • It worked thank you..
    – Isanka Thalagala
    Nov 11 at 8:30















up vote
0
down vote

favorite












I have a grid view and loop data from an array called objEntityType: EntityTypeVM; also I have created another an array equal to objEntityType as original dataset objEntityTypeOriginal: EntityTypeVM;



on search method, I'm filtering data on objEntityTypeOriginal and assign it to objEntityType.



But the problem is whatever I do to objEntityType same thins happen to objEntityTypeOriginal.



about my code below, In OnSearch() I need to search text so before that I need to empty the objEntityType But after I empty the objEntityType objEntityTypeOriginal also will empty.






import { Component, OnInit, Inject } from '@angular/core';
import { EntityTypeVM, EntityType, Entity, ApiResponseModel } from '../../../shared/models';


@Component({
selector: 'app-entity-create',
templateUrl: './entity-create.component.html',
styleUrls: ['./entity-create.component.scss']
})

export class EntityCreateComponent implements OnInit {
private objEntityTypeOriginal: EntityTypeVM;
private objEntityType: EntityTypeVM;

constructor() { }

ngOnInit() {

this.searchText = '';
this.objEntityType = new EntityTypeVM();
this.objEntityType.entities = [{ value: '001', synonyms: ['home', 'house']}];//this will replace with API data
this.objEntityTypeOriginal = = this.objEntityType;
}


EnterEntitySynonymsHandler(event: KeyboardEvent, index: number, synonym: string) {
if (event.key === 'Enter' && synonym) {
this.objEntityType.entities[index].synonyms.push(synonym);
this.objEntityType.entities[index].newSynonym = '';
this.objEntityTypeOriginal = = this.objEntityType;
}
}

RemoveSynonymItem(entityIndex, synonymIndex) {
this.objEntityType.entities[entityIndex].synonyms.splice(synonymIndex, 1);
this.objEntityTypeOriginal = = this.objEntityType;
}

// ----- problem In below method -----------------------
OnSearch() {
const searchText = this.searchText.toLowerCase();

if (searchText === '') {
this.objEntityType = this.objEntityTypeOriginal;
} else {

this.objEntityType.entities = ;//After this line .objEntityTypeOriginal.entities also will be empty.
this.objEntityTypeOriginal.entities.forEach(element => {
const itemAsString =JSON.stringify(element).toLowerCase();
if (itemAsString.indexOf(searchText) !== -1) {
this.objEntityType.entities.push(element);
}
});
}
}

}












share|improve this question






















  • Assuming the actual code is this.objEntityTypeOriginal = this.objEntityType;, this doesn't create a copy of the object. It assigns the object referenced by this.objEntityType to this.objEntityTypeOriginal. Those are thus two references to the same object. So when you modify the object referenced by on of the variables, the object referenced by the other is also modified: they both reference the exact same object.
    – JB Nizet
    Nov 11 at 7:40










  • Yes. whatever I do to one object same thing happens to other object. What is the solution for that..?
    – Isanka Thalagala
    Nov 11 at 8:03






  • 1




    Create a copy of the object (end its entities array) instead of just assigning it to another variable.
    – JB Nizet
    Nov 11 at 8:06










  • It worked thank you..
    – Isanka Thalagala
    Nov 11 at 8:30













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a grid view and loop data from an array called objEntityType: EntityTypeVM; also I have created another an array equal to objEntityType as original dataset objEntityTypeOriginal: EntityTypeVM;



on search method, I'm filtering data on objEntityTypeOriginal and assign it to objEntityType.



But the problem is whatever I do to objEntityType same thins happen to objEntityTypeOriginal.



about my code below, In OnSearch() I need to search text so before that I need to empty the objEntityType But after I empty the objEntityType objEntityTypeOriginal also will empty.






import { Component, OnInit, Inject } from '@angular/core';
import { EntityTypeVM, EntityType, Entity, ApiResponseModel } from '../../../shared/models';


@Component({
selector: 'app-entity-create',
templateUrl: './entity-create.component.html',
styleUrls: ['./entity-create.component.scss']
})

export class EntityCreateComponent implements OnInit {
private objEntityTypeOriginal: EntityTypeVM;
private objEntityType: EntityTypeVM;

constructor() { }

ngOnInit() {

this.searchText = '';
this.objEntityType = new EntityTypeVM();
this.objEntityType.entities = [{ value: '001', synonyms: ['home', 'house']}];//this will replace with API data
this.objEntityTypeOriginal = = this.objEntityType;
}


EnterEntitySynonymsHandler(event: KeyboardEvent, index: number, synonym: string) {
if (event.key === 'Enter' && synonym) {
this.objEntityType.entities[index].synonyms.push(synonym);
this.objEntityType.entities[index].newSynonym = '';
this.objEntityTypeOriginal = = this.objEntityType;
}
}

RemoveSynonymItem(entityIndex, synonymIndex) {
this.objEntityType.entities[entityIndex].synonyms.splice(synonymIndex, 1);
this.objEntityTypeOriginal = = this.objEntityType;
}

// ----- problem In below method -----------------------
OnSearch() {
const searchText = this.searchText.toLowerCase();

if (searchText === '') {
this.objEntityType = this.objEntityTypeOriginal;
} else {

this.objEntityType.entities = ;//After this line .objEntityTypeOriginal.entities also will be empty.
this.objEntityTypeOriginal.entities.forEach(element => {
const itemAsString =JSON.stringify(element).toLowerCase();
if (itemAsString.indexOf(searchText) !== -1) {
this.objEntityType.entities.push(element);
}
});
}
}

}












share|improve this question













I have a grid view and loop data from an array called objEntityType: EntityTypeVM; also I have created another an array equal to objEntityType as original dataset objEntityTypeOriginal: EntityTypeVM;



on search method, I'm filtering data on objEntityTypeOriginal and assign it to objEntityType.



But the problem is whatever I do to objEntityType same thins happen to objEntityTypeOriginal.



about my code below, In OnSearch() I need to search text so before that I need to empty the objEntityType But after I empty the objEntityType objEntityTypeOriginal also will empty.






import { Component, OnInit, Inject } from '@angular/core';
import { EntityTypeVM, EntityType, Entity, ApiResponseModel } from '../../../shared/models';


@Component({
selector: 'app-entity-create',
templateUrl: './entity-create.component.html',
styleUrls: ['./entity-create.component.scss']
})

export class EntityCreateComponent implements OnInit {
private objEntityTypeOriginal: EntityTypeVM;
private objEntityType: EntityTypeVM;

constructor() { }

ngOnInit() {

this.searchText = '';
this.objEntityType = new EntityTypeVM();
this.objEntityType.entities = [{ value: '001', synonyms: ['home', 'house']}];//this will replace with API data
this.objEntityTypeOriginal = = this.objEntityType;
}


EnterEntitySynonymsHandler(event: KeyboardEvent, index: number, synonym: string) {
if (event.key === 'Enter' && synonym) {
this.objEntityType.entities[index].synonyms.push(synonym);
this.objEntityType.entities[index].newSynonym = '';
this.objEntityTypeOriginal = = this.objEntityType;
}
}

RemoveSynonymItem(entityIndex, synonymIndex) {
this.objEntityType.entities[entityIndex].synonyms.splice(synonymIndex, 1);
this.objEntityTypeOriginal = = this.objEntityType;
}

// ----- problem In below method -----------------------
OnSearch() {
const searchText = this.searchText.toLowerCase();

if (searchText === '') {
this.objEntityType = this.objEntityTypeOriginal;
} else {

this.objEntityType.entities = ;//After this line .objEntityTypeOriginal.entities also will be empty.
this.objEntityTypeOriginal.entities.forEach(element => {
const itemAsString =JSON.stringify(element).toLowerCase();
if (itemAsString.indexOf(searchText) !== -1) {
this.objEntityType.entities.push(element);
}
});
}
}

}








import { Component, OnInit, Inject } from '@angular/core';
import { EntityTypeVM, EntityType, Entity, ApiResponseModel } from '../../../shared/models';


@Component({
selector: 'app-entity-create',
templateUrl: './entity-create.component.html',
styleUrls: ['./entity-create.component.scss']
})

export class EntityCreateComponent implements OnInit {
private objEntityTypeOriginal: EntityTypeVM;
private objEntityType: EntityTypeVM;

constructor() { }

ngOnInit() {

this.searchText = '';
this.objEntityType = new EntityTypeVM();
this.objEntityType.entities = [{ value: '001', synonyms: ['home', 'house']}];//this will replace with API data
this.objEntityTypeOriginal = = this.objEntityType;
}


EnterEntitySynonymsHandler(event: KeyboardEvent, index: number, synonym: string) {
if (event.key === 'Enter' && synonym) {
this.objEntityType.entities[index].synonyms.push(synonym);
this.objEntityType.entities[index].newSynonym = '';
this.objEntityTypeOriginal = = this.objEntityType;
}
}

RemoveSynonymItem(entityIndex, synonymIndex) {
this.objEntityType.entities[entityIndex].synonyms.splice(synonymIndex, 1);
this.objEntityTypeOriginal = = this.objEntityType;
}

// ----- problem In below method -----------------------
OnSearch() {
const searchText = this.searchText.toLowerCase();

if (searchText === '') {
this.objEntityType = this.objEntityTypeOriginal;
} else {

this.objEntityType.entities = ;//After this line .objEntityTypeOriginal.entities also will be empty.
this.objEntityTypeOriginal.entities.forEach(element => {
const itemAsString =JSON.stringify(element).toLowerCase();
if (itemAsString.indexOf(searchText) !== -1) {
this.objEntityType.entities.push(element);
}
});
}
}

}





import { Component, OnInit, Inject } from '@angular/core';
import { EntityTypeVM, EntityType, Entity, ApiResponseModel } from '../../../shared/models';


@Component({
selector: 'app-entity-create',
templateUrl: './entity-create.component.html',
styleUrls: ['./entity-create.component.scss']
})

export class EntityCreateComponent implements OnInit {
private objEntityTypeOriginal: EntityTypeVM;
private objEntityType: EntityTypeVM;

constructor() { }

ngOnInit() {

this.searchText = '';
this.objEntityType = new EntityTypeVM();
this.objEntityType.entities = [{ value: '001', synonyms: ['home', 'house']}];//this will replace with API data
this.objEntityTypeOriginal = = this.objEntityType;
}


EnterEntitySynonymsHandler(event: KeyboardEvent, index: number, synonym: string) {
if (event.key === 'Enter' && synonym) {
this.objEntityType.entities[index].synonyms.push(synonym);
this.objEntityType.entities[index].newSynonym = '';
this.objEntityTypeOriginal = = this.objEntityType;
}
}

RemoveSynonymItem(entityIndex, synonymIndex) {
this.objEntityType.entities[entityIndex].synonyms.splice(synonymIndex, 1);
this.objEntityTypeOriginal = = this.objEntityType;
}

// ----- problem In below method -----------------------
OnSearch() {
const searchText = this.searchText.toLowerCase();

if (searchText === '') {
this.objEntityType = this.objEntityTypeOriginal;
} else {

this.objEntityType.entities = ;//After this line .objEntityTypeOriginal.entities also will be empty.
this.objEntityTypeOriginal.entities.forEach(element => {
const itemAsString =JSON.stringify(element).toLowerCase();
if (itemAsString.indexOf(searchText) !== -1) {
this.objEntityType.entities.push(element);
}
});
}
}

}






typescript angular6






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 7:36









Isanka Thalagala

13010




13010












  • Assuming the actual code is this.objEntityTypeOriginal = this.objEntityType;, this doesn't create a copy of the object. It assigns the object referenced by this.objEntityType to this.objEntityTypeOriginal. Those are thus two references to the same object. So when you modify the object referenced by on of the variables, the object referenced by the other is also modified: they both reference the exact same object.
    – JB Nizet
    Nov 11 at 7:40










  • Yes. whatever I do to one object same thing happens to other object. What is the solution for that..?
    – Isanka Thalagala
    Nov 11 at 8:03






  • 1




    Create a copy of the object (end its entities array) instead of just assigning it to another variable.
    – JB Nizet
    Nov 11 at 8:06










  • It worked thank you..
    – Isanka Thalagala
    Nov 11 at 8:30


















  • Assuming the actual code is this.objEntityTypeOriginal = this.objEntityType;, this doesn't create a copy of the object. It assigns the object referenced by this.objEntityType to this.objEntityTypeOriginal. Those are thus two references to the same object. So when you modify the object referenced by on of the variables, the object referenced by the other is also modified: they both reference the exact same object.
    – JB Nizet
    Nov 11 at 7:40










  • Yes. whatever I do to one object same thing happens to other object. What is the solution for that..?
    – Isanka Thalagala
    Nov 11 at 8:03






  • 1




    Create a copy of the object (end its entities array) instead of just assigning it to another variable.
    – JB Nizet
    Nov 11 at 8:06










  • It worked thank you..
    – Isanka Thalagala
    Nov 11 at 8:30
















Assuming the actual code is this.objEntityTypeOriginal = this.objEntityType;, this doesn't create a copy of the object. It assigns the object referenced by this.objEntityType to this.objEntityTypeOriginal. Those are thus two references to the same object. So when you modify the object referenced by on of the variables, the object referenced by the other is also modified: they both reference the exact same object.
– JB Nizet
Nov 11 at 7:40




Assuming the actual code is this.objEntityTypeOriginal = this.objEntityType;, this doesn't create a copy of the object. It assigns the object referenced by this.objEntityType to this.objEntityTypeOriginal. Those are thus two references to the same object. So when you modify the object referenced by on of the variables, the object referenced by the other is also modified: they both reference the exact same object.
– JB Nizet
Nov 11 at 7:40












Yes. whatever I do to one object same thing happens to other object. What is the solution for that..?
– Isanka Thalagala
Nov 11 at 8:03




Yes. whatever I do to one object same thing happens to other object. What is the solution for that..?
– Isanka Thalagala
Nov 11 at 8:03




1




1




Create a copy of the object (end its entities array) instead of just assigning it to another variable.
– JB Nizet
Nov 11 at 8:06




Create a copy of the object (end its entities array) instead of just assigning it to another variable.
– JB Nizet
Nov 11 at 8:06












It worked thank you..
– Isanka Thalagala
Nov 11 at 8:30




It worked thank you..
– Isanka Thalagala
Nov 11 at 8:30

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246736%2farray-not-working-properly-when-removing-data-in-angular-6%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246736%2farray-not-working-properly-when-removing-data-in-angular-6%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma