Angular Http Subscribe Response Value Also Changes

Viewed 81

I'm requesting an api for the Angular Ionic project. I transfer the incoming data to two variables that are the same. But when I filter, the main parameter, "response," also changes.

When I wish" response " to remain the same, I want different filters to different parameter. Please help me.

//  
    venues: Venue
    kahvaltiMekani: Venue
// 
var searchVenuesSubscribe = this.searchVenueService.searchVenues(String(jsonParse.gitmeSebebi.join(",")).replace(" ", "+"), String(jsonParse.latLng));

searchVenuesSubscribe.pipe(map(imm=>{
          this.venues = imm
          this.venues.response.venues = this.venues.response.venues.filter((thing, i, arr) => {
            return arr.indexOf(arr.find(t => t.name === thing.name)) === i;
          });

          this.venues.response.venues.forEach(item => {
            try {
              this.distanceMatrix(jsonParse.lat, jsonParse.lng, item.location.lat, item.location.lng, item);
            }
            catch{ }
          });

          this.kahvaltiMekani = imm;
          this.kahvaltiMekani.response.venues = this.kahvaltiMekani.response.venues.filter(a => a.categories.find(b => b.id === "4bf58dd8d48988d179941735"
            || b.id === "4bf58dd8d48988d16a941735" || b.id === "52e81612bcbc57f1066b79f1" || b.id === "4bf58dd8d48988d143941735" || b.id === "52e81612bcbc57f1066b7a0c" || b.id === "4bf58dd8d48988d16d941735") !== undefined)
            .slice(0, 1);
        })).subscribe(response => {
        });
1 Answers

Okay. I think your issue is that a reference is created.

In JavaScript, it's just NOT possible to have a reference from one variable to another variable.

this.venues = imm

So in this case, this.venues will be a reference to imm ie whenever this.venues is changed, imm is also changed.

So try creating a copy of the object.

A method to create a copy of an object

JSON.parse(JSON.stringify(object))
Related