[英]how can I create buttons for values located inside an array, and that same array is located inside another array?
I am currently struggling with creating buttons for values inside an array, which are also located insinde an array.我目前正在努力为数组内的值创建按钮,这些按钮也位于数组内。 For example:例如:
let numbers = [{"allNum": ["0", "1", "2", "3","4"]}, {"evenNumbers": ["2", "4"]}];
So, in that array, lets say that I want to create buttons for all the values inside allNum.因此,在该数组中,假设我想为 allNum 中的所有值创建按钮。 Also, I know that in order to create a button I can simply use the createElement statement.另外,我知道为了创建一个按钮,我可以简单地使用 createElement 语句。 Will I need to use something like forEach() and a mix of createElement to achieve my goal?我是否需要使用类似 forEach() 的方法和混合使用 createElement 来实现我的目标?
Say for example, something like this?比如说,像这样的事情?
// lets say I have a html element with an id of "li"
var liEl= document.querySelector ("li");
function createBtn (allnum) = { document.createElement("button")
liEl.appendChild (createBtn);
return createBtn;
};
numbers.forEach (createBtn)
Thank you for your time!感谢您的时间!
Requires a little formatting on your behalf but I think this is what you were after.需要代表您进行一些格式化,但我认为这就是您所追求的。 You could of also gone down the foreach loop aswell:)您也可以进入 foreach 循环:)
for (x in numbers[0].allNum) {
let b = document.createElement("button");
b.setAttribute('id', x);
b.innerHTML = x;
document.body.appendChild(b);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.