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);
}
});
}
}
}
typescript angular6
add a comment |
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);
}
});
}
}
}
typescript angular6
Assuming the actual code isthis.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
add a comment |
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);
}
});
}
}
}
typescript angular6
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
typescript angular6
asked Nov 11 at 7:36
Isanka Thalagala
1319
1319
Assuming the actual code isthis.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
add a comment |
Assuming the actual code isthis.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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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