Title: JS how to do a double each.for
Tags: javascript;arrays;foreach
Question: for a gaming app, a player can select card types for his/her deck (first eachfor) and amount of each card type (2nd each.for). After this, I want to push this selection in an array.
Following part of my code works well:
//defining card types
let availableRoles = ["werwolf", "dorfbewohner", "seherin", "hexe", "jaeger", "armor", "heiler", "prinz"]
let gameState = {
roles: {}
//set card amount 0
;(() =&gt; {
availableRoles.forEach(role =&gt; gameState.roles[role] = 0)
//adding &amp; subtracting roles
const addRole = role =&gt; {
gameState.roles[role]++
document.getElementById(role + "_btn_remove").disabled = false;
document.getElementById(role + '_cnt').innerHTML = gameState.roles[role];
document.getElementById('total_cnt').innerHTML = totalNumberOfRolesInGame();
const removeRole = role =&gt; {
gameState.roles[role]--
if (gameState.roles[role] <= 0) {
gameState.roles[role] = 0
document.getElementById(role + "_btn_remove").disabled = true;
document.getElementById(role + '_cnt').innerHTML = gameState.roles[role];
document.getElementById('total_cnt').innerHTML = totalNumberOfRolesInGame();
const totalNumberOfRolesInGame = () =&gt; Object.values(gameState.roles).reduce((a,c) =&gt; a + c)
Now I want to hit every role and hit every number insider the role by using for each command. But it does not work.
var rollen = []
function Holmir() {
;(() =&gt; {
gameState.roles.forEach(element =&gt; element.forEach (myRole =&gt; rollen.push(myRole))
) })()
I'm thankful for any help!


Here is another answer: forEach works with an array.
gameState = {
roles: {}
gameState.roles gives you an object so forEach won't work with this

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:05/31/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript><arrays><foreach>', 'Title': 'JS how to do a double each.for', 'ViewCount': 39, 'id': 62113341, 'user_AboutMe': None, 'user_AccountId': 12991014.0, 'user_DisplayName': 'Christian Müller', 'user_Reputation': 1.0, 'user_Views': 1.0, 'user_id': 13647220.0}}