_reorder.js 900 B

123456789101112131415161718192021222324252627282930
  1. var copyArray = require('./_copyArray'),
  2. isIndex = require('./_isIndex');
  3. /* Built-in method references for those with the same name as other `lodash` methods. */
  4. var nativeMin = Math.min;
  5. /**
  6. * Reorder `array` according to the specified indexes where the element at
  7. * the first index is assigned as the first element, the element at
  8. * the second index is assigned as the second element, and so on.
  9. *
  10. * @private
  11. * @param {Array} array The array to reorder.
  12. * @param {Array} indexes The arranged array indexes.
  13. * @returns {Array} Returns `array`.
  14. */
  15. function reorder(array, indexes) {
  16. var arrLength = array.length,
  17. length = nativeMin(indexes.length, arrLength),
  18. oldArray = copyArray(array);
  19. while (length--) {
  20. var index = indexes[length];
  21. array[length] = isIndex(index, arrLength) ? oldArray[index] : undefined;
  22. }
  23. return array;
  24. }
  25. module.exports = reorder;