baseAt.js 963 B

1234567891011121314151617181920212223242526272829303132
  1. var isArrayLike = require('./isArrayLike'),
  2. isIndex = require('./isIndex');
  3. /**
  4. * The base implementation of `_.at` without support for string collections
  5. * and individual key arguments.
  6. *
  7. * @private
  8. * @param {Array|Object} collection The collection to iterate over.
  9. * @param {number[]|string[]} props The property names or indexes of elements to pick.
  10. * @returns {Array} Returns the new array of picked elements.
  11. */
  12. function baseAt(collection, props) {
  13. var index = -1,
  14. isNil = collection == null,
  15. isArr = !isNil && isArrayLike(collection),
  16. length = isArr ? collection.length : 0,
  17. propsLength = props.length,
  18. result = Array(propsLength);
  19. while(++index < propsLength) {
  20. var key = props[index];
  21. if (isArr) {
  22. result[index] = isIndex(key, length) ? collection[key] : undefined;
  23. } else {
  24. result[index] = isNil ? undefined : collection[key];
  25. }
  26. }
  27. return result;
  28. }
  29. module.exports = baseAt;