compactArray.js 524 B

12345678910111213141516171819202122232425262728
  1. /**
  2. * Copyright 2015-present Facebook. All Rights Reserved.
  3. *
  4. * @typechecks
  5. *
  6. */
  7. 'use strict';
  8. /**
  9. * Returns a new Array containing all the element of the source array except
  10. * `null` and `undefined` ones. This brings the benefit of strong typing over
  11. * `Array.prototype.filter`.
  12. */
  13. function compactArray(array) {
  14. var result = [];
  15. for (var i = 0; i < array.length; ++i) {
  16. var elem = array[i];
  17. if (elem != null) {
  18. result.push(elem);
  19. }
  20. }
  21. return result;
  22. }
  23. module.exports = compactArray;