29 May 2017

The snippet shows how to move item to the end of array. In a case where we have an array of objects and we want to move the array item by property value. In this example I am using lodash/underscore to find the index of the object and then move the array item by index.

Source code viewer
  1. // Find the index by property title, that should equal to "Unknown".
  2. var unknownKey = _.findIndex(results, ['title', 'Unknown']);
  3. // Make sure the key exists.
  4. if (unknownKey !== -1) {
  5. // Clone the value to the bottom of the array.
  6. results.push(results[unknownKey]);
  7. // Remove the original key.
  8. results.splice(unknownKey, 1);
  9. }
Programming Language: Javascript