İnteresting sorting pipe and database issue!


#1

I am getting data from local database and sort it at pipes .Somehow sorting is effecting the data is being feched from database

this is my ts file it takes data from provider

this.database.getdata("aaaaaaaa").then((res)=>{console.log("res:",res);Object.assign(this.contac,res);Object.assign(this.mock,res);;console.log("mock firts:",this.mock)}).then(()=>{console.log("gercek contac:",this.contac);console.log()}).then(()=>{ this.database.getdata("gruplar").then((res)=>{res.forEach(element => {
       this.gruplar.push(element);console.log("res:",res)
     });}).then(()=>{ loader.dismiss()

as you can see i got res from data base and it is an object .I combine it with an empty object which is contac.the data is {name:[B,A,C,D]}

I am expecting res is {name:[B,A,C,D]} and contac is {name:[A,B,C,D]}

BUT I am having res is like this {name:[A,B,C,D]} > INTERESTING

it s fine without sorting pipe but i want to sort contac on view

this is my HTML page

 <ion-item-sliding *ngFor="let item of contac.name|sorting">
        
    <ion-item  >
    <!--(press)="pressEvent($event)"-->
    <ion-label >{{item}}</ion-label>
     <ion-checkbox   [checked]=states.checktool  color="dark" (ionChange)="addtodata(item,$event)"></ion-checkbox>

Pipe.ts


@Injectable()
export class Sorting {
  /*
    Takes a value and makes it lowercase.
   */
  transform(array?: Array<string>, args?: string): Array<string> {
    // array.sort();
    if(array!==undefined){
    array.sort()
    }
    return array;
  }

}

ANY suggestion

Thankyou in advance


What s wrong about arrays and objects?
#2

I’m curious as to how this is taking place. From the behavior you describe, it looks like res andcontac are aliased to the same array.


#3

Let me give more explaination

This is ts file. i dont wan to work with res directly so i assigned res to contac and sort contac to be able to see it sorted at view.are not they seperated how can res be affected sort behavior .

res is an object consist of arrays . Contac is an empty object.

this.database.getdata("aaaaaaaa").then((res)=>{console.log("res:",res);Object.assign(this.contac,res)})


#4

I see your problem. You’ve cloned res into contac, but that’s only a shallow copy, so their name array is still shared. I would try something like lodash’s cloneDeep().


#5

Sounds interesting .You meant they are clone so but shared the same props .Really interesting . i ll check lodash thanks a lot