Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
deleted 108 characters in body
Source Link
Daniel
  • 4.5k
  • 2
  • 17
  • 40

I had to sort a list of objects - {name:'name', email:'email} by their keys, ex. by email, so they all be rendered with email key/value pair as a first key/value pair and I was surprised about the solution i came up to - Object.assign() when i loop through array and assign email key/value pair to each object:

  objects = [{name: 'name1', phone: 'phone1', email: 'email1'},
            {name: 'name1', phone: 'phone1', email: 'email1'},
            {name: 'name1', phone: 'phone1', email: 'email1'}]

  sort(val) {    
    for (let i=0; i < this.objects.length; i++) {
      let temp = {};
      temp[val] = null; 
      this.objects[i] = Object.assign(temp, this.objects[i])  
    }  
  }
}

The rest is done by Angular's ChangeDetection. Are there better ways or can you tell any possible issues with this? (I still don't feel very proficient in Javascript, so just wondering)

p.s. in real world objects comes from service and I slice() it, so I don't change it's global state.

Here is a StackBlitz's DEMO

I had to sort a list of objects - {name:'name', email:'email} by their keys, ex. by email, so they all be rendered with email key/value pair as a first key/value pair and I was surprised about the solution i came up to - Object.assign() when i loop through array and assign email key/value pair to each object:

  objects = [{name: 'name1', phone: 'phone1', email: 'email1'},
            {name: 'name1', phone: 'phone1', email: 'email1'},
            {name: 'name1', phone: 'phone1', email: 'email1'}]

  sort(val) {    
    for (let i=0; i < this.objects.length; i++) {
      let temp = {};
      temp[val] = null; 
      this.objects[i] = Object.assign(temp, this.objects[i])  
    }  
  }
}

The rest is done by Angular's ChangeDetection. Are there better ways or can you tell any possible issues with this? (I still don't feel very proficient in Javascript, so just wondering)

p.s. in real world objects comes from service and I slice() it, so I don't change it's global state.

Here is a StackBlitz's DEMO

I had to sort a list of objects - {name:'name', email:'email} by their keys, ex. by email, so they all be rendered with email key/value pair as a first key/value pair and I was surprised about the solution i came up to - Object.assign() when i loop through array and assign email key/value pair to each object:

objects = [{name: 'name1', phone: 'phone1', email: 'email1'},
            {name: 'name1', phone: 'phone1', email: 'email1'},
            {name: 'name1', phone: 'phone1', email: 'email1'}]

sort(val) {    
  for (let i=0; i < this.objects.length; i++) {
    let temp = {};
    temp[val] = null; 
    this.objects[i] = Object.assign(temp, this.objects[i])  
  }
}

The rest is done by Angular's ChangeDetection. Are there better ways or can you tell any possible issues with this? (I still don't feel very proficient in Javascript, so just wondering)

Here is a StackBlitz's DEMO

Source Link

Angular, sort objects by their keys

I had to sort a list of objects - {name:'name', email:'email} by their keys, ex. by email, so they all be rendered with email key/value pair as a first key/value pair and I was surprised about the solution i came up to - Object.assign() when i loop through array and assign email key/value pair to each object:

  objects = [{name: 'name1', phone: 'phone1', email: 'email1'},
            {name: 'name1', phone: 'phone1', email: 'email1'},
            {name: 'name1', phone: 'phone1', email: 'email1'}]

  sort(val) {    
    for (let i=0; i < this.objects.length; i++) {
      let temp = {};
      temp[val] = null; 
      this.objects[i] = Object.assign(temp, this.objects[i])  
    }  
  }
}

The rest is done by Angular's ChangeDetection. Are there better ways or can you tell any possible issues with this? (I still don't feel very proficient in Javascript, so just wondering)

p.s. in real world objects comes from service and I slice() it, so I don't change it's global state.

Here is a StackBlitz's DEMO