0

I have this array...

public incidents: any[] = [
  {
    id: 1,
    name: "Default Case Set",
    type: "CASEWORK",
    str: 34,
    mtdna: 0,
    ystr: 0,
    xstr: 0,
    snps: 0
  }
]

I'm passing it into a modal like this...

public openEditModal(id: number): void {

  this.incidentToBeEdited = this.incidents.filter(result => result.id == id).pop();

  const initialState: ModalOptions = {
    initialState: {
      items: this.incidentToBeEdited,
      title: 'Edit Incident'
    }
  };

  // Close modal
  this.bsModalRef = this.modalService.show(EditModalComponent, initialState);
}

The problem is that the keys in the object in the incidents array are automatically alphabetized.

When I console the "this.incidentToBeEdited" variable, I get this...

{
    mtdna: 0
    name: "Default Case Set"
    snps: 0
    str: 34
    type: "CASEWORK"
    xstr: 0
    ystr: 0
}

So the object that gets sent to the modal (for display purposes) is automatically alphabetized.

I don't want this because I want the fields to appear as they do in the table, which is how they are in the original incidents array.

Is there anyway I can override Angular's need to alphabetize an object?

Here is the code for EditModalComponent...

export class EditModalComponent implements OnInit {

  constructor(
    public bsModalRef: BsModalRef,
    private http: HttpClient,
    private formBuilder: FormBuilder) {
    this.items = this.items;
    this.bsModalRef = this.bsModalRef;

    this.editModal = this.formBuilder.group({});    
  }

  // Page loading properties

  public httpRequestInProgress: boolean = false;
  public pageLoaded: boolean = false;
  public pageLoadError: string = '';
  public pageLoading: boolean = true;

  // Properties

  public editModal: FormGroup;

  public items?: any;

  public title?: string;

  // Methods

  ngOnInit(): void {

    this.editModal = this.formBuilder.group(
      this.items
    )
    console.log("this.items", this.items);

    // Remove id from list of items
    const itemsInAnArray = [this.items];
    itemsInAnArray.forEach((item: any) => delete item.id);

    this.pageLoading = false;
    this.pageLoaded = true;

  }

}

Here is the HTML for EditModalComponent...

<form [formGroup]="editModal" *ngIf="this.items">

        <div class="row">

          <div class="col-sm-12">

            <div *ngFor="let item of this.items | keyvalue">

              <div class="col-sm-12 mb-3">
                <label class="text-capitalize" for="firstName">{{item.key}}</label>
                <input class="form-control"
                        id="{{item.key}}"
                        value="{{item.value}}"
                        formControlName="{{item.key}}">
              </div>

            </div>          

          </div>

          <div class="mt-3">
            <button class="btn btn-primary float-start"
                    type="button"
                    (click)="saveAsync()">
              Save
            </button>
            <button class="btn btn-secondary me-1 float-start"
                    type="button"
                    (click)="bsModalRef.hide()">
              Cancel
            </button>
          </div>

        </div>

      </form>
    ```
Code Junkie
  • 83
  • 1
  • 15
  • 2
    It is not angular, Object order is not guaranteed. https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order – epascarello Feb 22 '22 at 16:11
  • Why does it matter what order the properties are in? – evolutionxbox Feb 22 '22 at 16:17
  • Because I want the fields to appear as they do in the table, which is how they are in the original incidents array. – Code Junkie Feb 22 '22 at 16:30
  • If you want to control or preserve order, you’d need to use an array.. – MikeOne Feb 22 '22 at 17:24
  • I am using an array. But I'm talking about the order of the keys in the object that's inside the array. – Code Junkie Feb 22 '22 at 20:04
  • @CodeJunkie please share the template of `EditModalComponent`, I want to know how you're using initialState to display that object in template – Yogi Feb 23 '22 at 13:00
  • @Yogi I updated the code above to include the code for EditModalComponent. – Code Junkie Mar 01 '22 at 16:34
  • @epascarello It might be time to let that one go. https://stackoverflow.com/q/30076219/215552 As of ES2020, it's pretty much guaranteed to be insertion order, except for integer-coercible keys – Heretic Monkey Mar 01 '22 at 17:17
  • 1
    I think @MikeOne is taking about using an array for the key order or the object that's in the array. Something like `const order = [ 'id', 'name','type','str','mtdna','ystr','xstr','snps']` – Heretic Monkey Mar 01 '22 at 17:19
  • @CodeJunkie thank you for sharing the code, can you please also share part of the HTML of `EditModalComponent` where you're iterating over incidents? and I noticed you're assigning this.items to itself in first line of constructor, correct me If I'm wrong, I think it's a mistake and this is the property that stores list of incidents you received in modalRef? – Yogi Mar 02 '22 at 19:37
  • @HereticMonkey it's still not a good idea to depend on it, the top answer in question you linked mentions "That doesn't change the fact that using property order for fundamental program logic probably isn't a good idea, since the order for non-integer-index properties depends on when the properties were created." and I agree with it, if someone later changes the assignment order in object then that'll also affect property enumeration, I think it's better to remain explicit by using an array to mention the exact order in which properties should be displayed on UI. – Yogi Mar 02 '22 at 19:44
  • @Yogi, I've updated the code above to include the html for EditModalComponent... – Code Junkie Mar 02 '22 at 20:09
  • @Yogi Sure, I was talking about letting go of the assertion that objects don't have guaranteed property order. They do; insertion order (with caveats). Whether it makes sense to build applications that depend on the order is a different topic. Something I hoped I help clear up with my following comment (despite mistyping "for" as "or" the second time I used it). – Heretic Monkey Mar 02 '22 at 21:35

0 Answers0