I'm working on a function that loops over an array (with arrays) and then replaces the text on a few divs with the value of index 1 of the nested arrays.
But only with these conditions:
- the nested array needs to have a specific (string) value on index 0 (which is 'categorie 1')
- it should only give the value of index 1 of the nested array if the value of index 1 is a unique value (I mean, if there are other nested arrays with the same value inside index 1, it is not unique. I do not mean unique inside the nested array itself).
The function get's activated by an event handler (clicking on 'Categorie 1', see codepen). When clicking on categorie 1, the following values: themaA1 themaA2 themaA3 themaA4 themaA5 themaA6 (each value in a different div) should be visible inside the divs of the green container (see codepen).
Unfortunately I'm not able to get it work.
What does work is looping and putting data in the different divs which shares the same value on index 0. See code below.
function displayThemes() {
let theme = "i";
for (let i = 0; i < forms.length; i++) {
theme = forms[i][1];
if (forms[i][0] === "Categorie 1")
document.getElementById("theme" + i).innerHTML = theme;
}
}
What does not work is getting the right data in the divs (only the unique values).
I got this far (see code below) but it does not work unfortunately.
function displayThemes() {
let theme = "i";
for (let i = 0; i < forms.length; i++) {
theme = forms[i][1];
if (forms[i][0] === "Categorie 1" && forms[i][1] !== theme)
document.getElementById("theme" + i).innerHTML = theme;
}
}
Here is the full code with interface: https://codepen.io/cheizer66/pen/pobXBmy
I'm not sure why it does not work. What am I doing wrong? What should you do differently? What could make it work? I hope I stated the problem clear enough and looking forward to your respons!
Best regards,
OK
ps: Yes, I'm new to javascript and programming