{"id":22194683,"url":"https://github.com/xnf/recursively","last_synced_at":"2025-03-24T21:28:47.235Z","repository":{"id":57110622,"uuid":"90893561","full_name":"xnf/recursively","owner":"xnf","description":"Recursively run a callback function on each item in an array","archived":false,"fork":false,"pushed_at":"2017-05-11T11:17:46.000Z","size":14,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-25T23:02:45.145Z","etag":null,"topics":["javascript","recursion","recursive","recursive-functions"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xnf.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-10T17:59:06.000Z","updated_at":"2017-05-11T10:44:17.000Z","dependencies_parsed_at":"2022-08-21T07:00:11.722Z","dependency_job_id":null,"html_url":"https://github.com/xnf/recursively","commit_stats":null,"previous_names":["edgarszagorskis/recursively"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xnf%2Frecursively","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xnf%2Frecursively/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xnf%2Frecursively/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xnf%2Frecursively/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xnf","download_url":"https://codeload.github.com/xnf/recursively/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245354450,"owners_count":20601546,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","recursion","recursive","recursive-functions"],"created_at":"2024-12-02T13:14:02.353Z","updated_at":"2025-03-24T21:28:47.214Z","avatar_url":"https://github.com/xnf.png","language":"JavaScript","readme":"Recursively JavaScript\n==========\n\n[![Build Status](https://travis-ci.org/EdgarsZagorskis/recursively.svg?branch=master)](https://travis-ci.org/EdgarsZagorskis/recursively) [![Coverage Status](https://coveralls.io/repos/github/EdgarsZagorskis/recursively/badge.svg)](https://coveralls.io/github/EdgarsZagorskis/recursively)\n\nRecursively run JavaScript callback function on each item in an array.\n\nRecursion happens if the item is also an array - for example, as in 2D arrays.\n\nOther example for a recursion are arrays of objects with nested objects - for example a menu with submenus.\n\n## Installation\n \n    npm install recursively -S\n    \n## Usage    \n   \n    var recursively = require('recursively');\n    \n    \n    /**\n     * Recursively run callback on items in a Javascript array\n     * @param arr               An array that can be iterated\n     * @param callback          Callback will receive following arguments:\n     *                          item = value or item in the collection\n     *                          index = index of this item in the array\n     *                          arr = original array. So one can change the original value if needed\n     * @param childProperty     Optional child property. If iterated item is an object, recursion will dive into this property\n     * @return void | any       If callback returns a value, it is rewriting the value in array or else it is left intact\n     */\n    recursively(input: array, callback: (item?:any, index?:number, childProperty?:array) =\u003e void|any)\n    \n### Example 1 - populating 3x3 array with incremental integers from 1 to 9\n\n    var recursively = require('recursively');\n    \n    var data = [new Array(3).fill(), new Array(3).fill(), new Array(3).fill()];\n    var i = 1;\n    recursively(data, function () {\n        return i++;\n    }           \n    \n    // data will be [[1, 2, 3], [4, 5, 6], [7, 8, 9]];\n        \n### Example 2 - populate array of 9 items with fibonacci numbers by referring to the previous item in collection\n\n    var recursively = require('recursively');\n    \n    var data = new Array(9).fill();\n    recursively(data, function (item, index, collection) {\n        if (index === 0) {\n            return 0;\n        } else if (index === 1) {\n            return 1;\n        } else {\n            return collection[index - 2] + collection[index - 1];\n        }\n    });          \n    \n    // data will be [[1, 2, 3], [4, 5, 6], [7, 8, 9]];\n    \n### Example 3 - enabling all objects in flat array\n\n    var recursively = require('recursively');\n    \n    var data = [{enabled: false}, {enabled: false}];\n    recursively(data, function(item){\n        item.enabled = true;\n    });\n    \n    // data will be [{enabled: true}, {enabled: true}];\n        \n### Example 4 - enabling first child of each submenu\n\n    var recursively = require('recursively');\n    \n    var menu = [{\n        title: 'Level 1',\n        enabled: false,\n        submenu: [\n            {\n                title: 'Level2 a',\n                enabled: false,\n                submenu: [\n                    {\n                        title: 'Level3 aa',\n                        enabled: false\n                    },\n                    {\n                        title: 'Level3 ab',\n                        enabled: false\n                    }]\n            }, {\n                title: 'Level2 b',\n                enabled: false,\n                submenu: [\n                    {\n                        title: 'Level2 ba',\n                        enabled: false\n                    },\n                    {\n                        title: 'Level2 bb',\n                        enabled: false\n                    }]\n            }]\n    }];\n    \n    function enableFirstChild(item, index) {\n        if (index === 0) {\n            item.enabled = true;\n        }\n    }\n\n    recursively(menu, enableFirstChild, 'submenu');\n    \n    // menu[0].enabled==true;\n    // menu[0].submenu[0].enabled==true;\n    // menu[0].submenu[0].submenu[0].enabled==true;\n    // menu[0].submenu[1].submenu[0].enabled==true;\n\n    \n## Test\n\n    npm test\n    \n## Contributions\n\nwelcomed.... Just make a test.","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxnf%2Frecursively","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxnf%2Frecursively","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxnf%2Frecursively/lists"}