Yes, I know this question has been asked a million times before. However, I haven't found any helpful answers in those previous questions.
Here's my dilemma: I have two arrays containing two different objects, each with a unique string property used to identify them. This property would be the key to sort them, but the names of the properties in the objects are not the same (accessValue, modifiedOption). Despite this, their values are identical.
Object1: { ... accessValue, ... };
Object2: { ..., modifiedOption, ... };
array1: Object1[];
array2: Object2[];
My goal is to sort array1 based on the order of objects in array2. Essentially, I want all items in array1 to be in the same order as array2.
These arrays are part of a connected dropdown selection system that can be dynamically updated. However, adding new elements is causing issues (the last added item is not placed at the end). This may be due to the filter function in the code below:
When adding new dropdowns:
addFieldRow() {
this.fieldRows.push(0); // Indicates an unknown selection, populating the array to display a second dropdown
...
}
public onSelect() {
this.fieldRows = this.editOptions.filter(
option => this.selectedOptions.some(el => el.modifiedOption === option.accessValue)
);
this.disableSelected();
this.optionSelected = true;
}
Therefore, I need to either fix my addRow logic or implement a sorting utility to ensure that the objects in fieldRows are ordered the same as the selectedOptions array, as it directly corresponds to the selection model.
Unfortunately, I can't provide a StackBlitz example as it's difficult to replicate my current situation.