Reorder array of object sequence value [closed]

  data-structures, javascript, logic

I have an array of object with containing some batch information and sequence no. Sequence is between 1 to length of the array. From the UI, this data structure is mapped with a Drag and Drop data table.

var Temp = [{
        'name': 'Batch1',
        'Sequence': 1
    }, {
        'name': 'Batch2',
        'Sequence': 2
    }, {
        'name': 'Batch3',
        'Sequence': 3
    }, {
        'name': 'Batch4',
        'Sequence': 4
    }]

As an example.. when Batch4 column drag and drop to Batch1, Sequence of all these Batches should be change accordingly.

var Temp = [{
        'name': 'Batch1',
        'Sequence': 2
    }, {
        'name': 'Batch2',
        'Sequence': 3
    }, {
        'name': 'Batch3',
        'Sequence': 4
    }, {
        'name': 'Batch4',
        'Sequence': 1
    }]

This is the chnageEvent

reorderBatch(batchId, prev_seq, new_seq){
    
}

Can you please recommend a best and fast logic to change the sequence of this array of objects

Source: Ask Javascript Questions

LEAVE A COMMENT