-2

On what I'm working right now is a little bit complex for my brain. I've got some data, and that data has a field 'position', and based on that position they are going to be displayed on the client side in that order(for example, for the last item user has added, he can change its position to be 1 and on client side it will be displayed first, then comes rest of the data), and he can always change the position(from 1 to 8, and then it will be displayed the last because that number of data is limited to maximum of 8). But the problem is when user changes the position for example from 4 to 1, and there already is a data with position 1, so then we have to items with the same position, which should not happen. Is there a solution to go over the array, and check for same values and then replace them?

Example: There are 2 items, item 1 has position 1 and item 2 has position 2. If we change item 2 position to 1, then both of them will have one, but that item 1 should automatically auto increment to 2.

What I tried so far was do forEach on the array, and check values with conditions but its not working the best. Is there some algorithm to accomplish this?

this.items.forEach((itemRes) => {
      let itemDash = result;
      if (itemRes.position === result.ordinal) {
        if(itemRes.position !== result) {
          itemRes.ordinal++;
        }
      } else if (itemRes.position === this.items.length && itemRes.ordinal >= 8) {
        itemRes.position--;
      }
    })

Here is my code for checking and changing array items and their positions.

Boris
  • 602
  • 2
  • 8
  • 29
  • 1
    Welcome to SO. Please provide a [mcve]. For example, why not share the code for your initial attempt? In addition, it is helpful to describe your problem succinctly, focusing on the problematic part. – jpp Feb 02 '18 at 15:38
  • share your code – Dezigo Feb 02 '18 at 15:44

1 Answers1

1

Kudos to this gist: https://gist.github.com/albertein/4496103

If I am understanding you correctly, you would want something like presented in the link, so when "TypeScriptifying" it and making it applicable to your (simplified) case:

array = [1, 2, 3, 4, 5];

move(array, element, delta) {
  let index = array.indexOf(element);
  let newIndex = index + delta;

  //Already at the top or bottom.
  if (newIndex < 0 || newIndex == array.length) return; 

  let indexes = [index, newIndex].sort(); //Sort the indexes
  //Replace from lowest index, two elements, reverting the order
  array.splice(indexes[0], 2, array[indexes[1]], array[indexes[0]]); 
}

moveUp(element) {
  this.move(this.array, element, -1);
}

moveDown(element) {
  this.move(this.array, element, 1);
}

And the corresponding HTML:

<div *ngFor="let a of array">
  {{a}} 
  <button (click)="moveUp(a)">Move Up</button>
  <button (click)="moveDown(a)">Move Down</button>
</div>

StackBlitz

This can also give you some inspiration: Move an array element from one array position to another :)

AT82
  • 71,416
  • 24
  • 140
  • 167