0

I have the following data structure:

var list = [
   {id: 156, next_item_position: 2, name: 'Item #1'}, 
   {id: 157, next_item_position: 3, name: 'Item #2'}, 
   {id: 158, next_item_position: 4, name: 'Item #3'}, 
   {id: 159, next_item_position: 5, name: 'Item #4'}, 
   {id: 160, next_item_position: 6, name: 'Item #5'}, 
   {id: 161, next_item_position: 7, name: 'Item #6'}, 
   {id: 162, next_item_position: null, name: 'Item #7'}
];

There's a property named next_item_position, basically it should store the position of "next" item (it can be selected by user).

Now, I want to keep the "connection" between items when their order is change (we have a UI for changing positions).

So, for example:

  1. Item with ID (156) have next_item_position equals to 2 (2nd item).
  2. The order of second item is changed from 2 to 3 (2nd to 3rd).
  3. Now I want item with ID (156) to point to 3rd position instead of 2nd.

My implementation looks like this:

  // The magic should happens here
  for(j = 0, jj = list.length; j < jj; j++) {
    var next_position = list[j].next_item_position;

    // Find ID of item based on its position
    var item_id = defaultPositions[next_position];

    // Now, find current position of "item_id"
    var current_position = currentPositions.indexOf(item_id);

    // And update the property
    list[j].next_item_position = current_position;
  }

  // Update "defaults" array
  defaultPositions = currentPositions;

But, unfortunately, it's not working as expected.

Fiddle: https://jsfiddle.net/fhuk31q2/6/

1

2 Answers 2

0

Something like this?

function moveItem ( list, id, to_pos ) {
    let list_copy = list.concat ( [] );
    let itemIndex = list_copy.findIndex ( x => { return x.id === id } );
    let item = list_copy.splice ( itemIndex, 1 ) [ 0 ];
    list_copy.splice ( to_pos, 0, item );
    list_copy = list_copy.map ( ( x, i ) => { x.next_item_position = i + 2; return x; } );
    return list_copy;
}

console.log ( moveItem ( list, 156, 3 ) );

This will take the item with id 156 (so index 0), move it to whatever index you specify, and reorder all the "next" references.

From you description, here's what I get:

  • Next_item will always be the next item in the list. So every time an item is moved, they all need to be updated (say for example, you moved the last one to the first position, which would move the old first to the second, and so on).
  • Copy the list to avoid mutating the global model. You can dispense with this if you don't need it.

Note that I agree a different data structure may suit you here (probably a linked list). But if that's not feasible or practical in your context (say for example the speed of lookups mattered, then an array might be a better choice), then just move the item in the array and remap the refer_to_nexts based on the new array index (I use +2 because it seems you want a "1" based index, and nothing refers to "1" as "next").

0

Expanding on my comment. This might help you get started.

var Node = function(id, name, previous) {

  this.previous = previous;
  if (previous) {
    previous.next = this;
  }
  this.id = id;
  this.name = name;

};

var swap = function(n1, n2) {

  var n1previous = n1.previous;
  var n1next = n1.next;

  n1.next = n2.next;
  if (n1.next) {
    n1.next.previous = n1;
  }
  n1.previous = n2.previous;
  if (n1.previous) {
    n1.previous.next = n1;
  }

  n2.next = n1next;
  if (n2.next) {
    n2.next.previous = n2;
  }
  n2.previous = n1previous;
  if (n2.previous) {
    n2.previous.next = n2;
  }

}
var walk = function(node) {
  if (node) {
    console.log(node.id);
    walk(node.next);
  }
}

var n1 = new Node(156, 'Item #1');
var n2 = new Node(157, 'Item #2', n1);
var n3 = new Node(158, 'Item #3', n2);
var n4 = new Node(159, 'Item #4', n3);
var n5 = new Node(160, 'Item #5', n4);
var n6 = new Node(161, 'Item #6', n5);
var n7 = new Node(162, 'Item #7', n6);

console.log('Walk');
walk(n1);

swap(n3, n7);

console.log('Walk after swap');
walk(n1);

You can also use this library instead of writing your own code: http://mauriciosantos.github.io/Buckets-JS/symbols/buckets.LinkedList.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.