Home

function: takeWhile


takeWhile(array: Array, predicate: function): Array

Creates a slice of `array` with elements taken from the beginning. Elements are taken until `predicate` returns falsey. The predicate is invoked with three arguments: (value, index, array).

Example(s):

const users = [
  { 'user': 'barney',  'active': true },
  { 'user': 'fred',    'active': true },
  { 'user': 'pebbles', 'active': false }
]

takeWhile(users, ({ active }) => active)
// => objects for ['barney', 'fred']

Params:

Name Type Attribute Description
array The array to query.
predicate The function invoked per iteration.

Returns:

Array

  {
    "comment": "/**\r\n * Creates a slice of `array` with elements taken from the beginning. Elements\r\n * are taken until `predicate` returns falsey. The predicate is invoked with\r\n * three arguments: (value, index, array).\r\n *\r\n * @since 3.0.0\r\n * @category Array\r\n * @param {Array} array The array to query.\r\n * @param {Function} predicate The function invoked per iteration.\r\n * @returns {Array} Returns the slice of `array`.\r\n * @example\r\n *\r\n * const users = [\r\n *   { 'user': 'barney',  'active': true },\r\n *   { 'user': 'fred',    'active': true },\r\n *   { 'user': 'pebbles', 'active': false }\r\n * ]\r\n *\r\n * takeWhile(users, ({ active }) => active)\r\n * // => objects for ['barney', 'fred']\r\n */",
    "meta": {
        "range": [
            737,
            865
        ],
        "filename": "takeWhile.js",
        "lineno": 24,
        "path": "C:\\Users\\beaujeup\\projects\\jsdoc-template\\examples\\lodash\\lodash-repo",
        "code": {
            "id": "astnode100009069",
            "name": "takeWhile",
            "type": "FunctionDeclaration",
            "paramnames": [
                "array",
                "predicate"
            ]
        }
    },
    "description": "Creates a slice of `array` with elements taken from the beginning. Elements\rare taken until `predicate` returns falsey. The predicate is invoked with\rthree arguments: (value, index, array).",
    "since": "3.0.0",
    "tags": [
        {
            "originalTitle": "category",
            "title": "category",
            "text": "Array",
            "value": "Array"
        }
    ],
    "params": [
        {
            "type": {
                "names": [
                    "Array"
                ]
            },
            "description": "The array to query.",
            "name": "array"
        },
        {
            "type": {
                "names": [
                    "function"
                ]
            },
            "description": "The function invoked per iteration.",
            "name": "predicate"
        }
    ],
    "returns": [
        {
            "type": {
                "names": [
                    "Array"
                ]
            },
            "description": "Returns the slice of `array`."
        }
    ],
    "examples": [
        "const users = [\r  { 'user': 'barney',  'active': true },\r  { 'user': 'fred',    'active': true },\r  { 'user': 'pebbles', 'active': false }\r]\r\rtakeWhile(users, ({ active }) => active)\r// => objects for ['barney', 'fred']"
    ],
    "name": "takeWhile",
    "longname": "takeWhile",
    "kind": "function",
    "scope": "global",
    "___id": "T000002R000822",
    "___s": true,
    "filepath": "takeWhile.js"
}