Skip to content

Instantly share code, notes, and snippets.

@dilunika
Last active July 24, 2017 17:23
Show Gist options
  • Save dilunika/02121b080c2b7cf54d381246a7418ca8 to your computer and use it in GitHub Desktop.
Save dilunika/02121b080c2b7cf54d381246a7418ca8 to your computer and use it in GitHub Desktop.
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));
@shulgatyy
Copy link

function rearrangeItems(items, moveFromIndex, moveToIndex) {
	const copy = [...items]; // const copy = items.slice();
	const [movingItem] = copy.splice(moveFromIndex, 1);
	copy.splice(moveToIndex, 0, movingItem);

	return copy;
}
-const update = require('immutability-helper');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment