I'm still new to JavaScript, so apologies if anything is unclear (or if this is really simple) - basically I am trying to create a list from a JSON array, which adds a number of list items to a list (where the exact number of list items to be added varies). For example, in the below - in the "roles" array there are three list items that need to be added for John Smith and four for Mary Taylor.
rolesData = [
{"name": "John Smith",
"title": "project manager",
"roles":
["Planning and Defining Scope", "Activity Planning and Sequencing", "Resource Planning"],
},
{"name": "Mary Taylor",
"title": "test analyst",
"roles":
["design and develop tests for software and systems to detect faults", "analyse the defects and bugs to identify what is causing them", "track the success of the solutions", "keep software and systems documentation up to date"],
},
Here is the loop I use to add the list items. For the example above this works as intended for the second one as there are 4 items to be added and it loops through four times, but it will obviously add 4 items to the one that only requires 3 as well (the last one comes up as undefined) - what I'm struggling a little with is how to make it so that it loops through the number of times that corresponds to how many items there are in "roles" - e.g. if there are five items in "roles", it should add 5 list items. i.e. what should the condition be in the for loop: for (i = 0; ???????; i++)?
NB: 'num' is a random number that is generated, so if num is 0 it will get the data for John Smith etc.
function createList(num) {
...
var rolesList = document.createElement("ul");
for (i = 0; i < 3; i++){
var roleItem = document.createElement("li");
roleItem.innerHTML = rolesData[num].roles[i];
rolesList.appendChild(roleItem);
}
Update: Having solved the issue of how to get the list to populate with the correct numbers on loading the page, I am now trying to add a functionality where by if you click on a button it will change the items in the list - this means that it may change from having 3 items to 4 items for example, and vice versa.
I have tried adapting the answer from @ggorlen (which worked great for creating the list) below but using getElementById rather than create element (when creating the list I assigned the id "featuredList" to the ul and "listItem" + count to the li's, so that for each new listItem that was created the id was listItem1, listItem2 etc. - I've checked this in console.log and appeared to be working correctly). I believe the issue is that if the initial list has only 3 items then there are only 3 listItem id's assigned, so if trying to change the list to one with 4 in it, there is no id for the fourth one to go to - so I guess my question is how would I do it so that if you are changing the elements, you can change it to a list with more or less items in it?
const createList = data => {
const list = document.getElementById("featuredList");
count = 1;
data.forEach(e => {
const listItem = document.getElementByID("listItem" + count);
listItem.innerHTML = e;
list.appendChild(listItem);
count++;
});
return list;
};
forEachinstead