utils/array-utils/src/nth.js

  1. /**
  2. * Return the Nth element of the given array.
  3. * @param {*} array - anything
  4. * @param {Number} index - index of the element to return
  5. * @returns {*} Nth element of the array, or undefined
  6. * @alias module:array-utils.nth
  7. * @example
  8. * let value = nth([1], 2) // undefined
  9. * let value = nth([1, 2, 3, 4, 5], 3) // 4
  10. */
  11. const nth = (array, index) => {
  12. if (!Array.isArray(array) || array.length < index) {
  13. return undefined
  14. }
  15. return array[index]
  16. }
  17. module.exports = nth