partitionArray.js 733 B

12345678910111213141516171819202122232425262728293031
  1. "use strict";
  2. /**
  3. * Copyright (c) 2013-present, Facebook, Inc.
  4. *
  5. * This source code is licensed under the MIT license found in the
  6. * LICENSE file in the root directory of this source tree.
  7. *
  8. * @typechecks
  9. *
  10. */
  11. /**
  12. * Partitions an array given a predicate. All elements satisfying the predicate
  13. * are part of the first returned array, and all elements that don't are in the
  14. * second.
  15. */
  16. function partitionArray(array, predicate, context) {
  17. var first = [];
  18. var second = [];
  19. array.forEach(function (element, index) {
  20. if (predicate.call(context, element, index, array)) {
  21. first.push(element);
  22. } else {
  23. second.push(element);
  24. }
  25. });
  26. return [first, second];
  27. }
  28. module.exports = partitionArray;