Last active
July 24, 2017 17:23
-
-
Save dilunika/02121b080c2b7cf54d381246a7418ca8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const update = require('immutability-helper'); | |
const items = [ | |
{key: 'IT-1', title: 'Dummy Item 1'}, | |
{key: 'IT-2', title: 'Dummy Item 2'}, | |
{key: 'IT-3', title: 'Dummy Item 3'}, | |
{key: 'IT-4', title: 'Dummy Item 4'}, | |
{key: 'IT-5', title: 'Dummy Item 5'}, | |
{key: 'IT-6', title: 'Dummy Item 6'} | |
]; | |
function rearrangeItems(items, moveFromIndex, moveToIndex) { | |
const movingItem = items[moveFromIndex]; | |
const arrangedArray = update(items, {$splice: [ | |
[moveFromIndex, 1], | |
[moveToIndex, 0, movingItem] | |
]}); | |
return arrangedArray; | |
} | |
const arrangedArray = rearrangeItems(items, 2, 0); | |
console.log('Rearranged Items : ', JSON.stringify(arrangedArray)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-const update = require('immutability-helper');