I am trying to get the number of items in an array then run a function that amount of times. Is this possible in JavaScript? I am storing the array in JSON.
This is the code I have set up for when there is only 1 app
if (localStorage.getItem("repo")) {
repo = JSON.parse(localStorage.getItem("repo"));
create_app(repo.app.appName, repo.app.appIcon, repo.app.appId, repo.app.appUrl);
};
This is what the JSON looks like. The array I am trying to access is called "apps".
{
"repoName": "repo_name",
"repoId": "unique_id",
"repoVer": "1",
"apps": [{
"appName": "first app name"
"appId": "app1",
"appUrl": "https://example.com",
"appIcon": "https://example.com/favicon.ico"
},
{
"appName": "second app name",
"appId": "app2",
"appUrl": "https://example.org",
"appIcon": "https://example.org/favicon.ico"
}]
}
CodePudding user response:
repo.apps.length will give you the number of items in apps
Also, since you've already parsed your data into JSON object repo, it now can understand your repo.apps is an array and you can just loop over it directly likes:
repo.apps.forEach((app) => {})
or
for (let app in repo.apps) {}
etc
Hope this help
