简体   繁体   English

如何为位于数组内的值创建按钮,而同一数组位于另一个数组内?

[英]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.

相关问题 如何过滤位于另一个数组内部的数组的第 n 个元素? - how can I filter the nth element of an array located inside of another array? 我如何创建一个函数来更改位于数组内的对象内的属性,在 JSON 中 - How do i create a function that will change the property inside an object that is located inside of an array, in a JSON 如何向位于数据阵列内的图像添加超链接? - How do I add a hyperlink to an image located inside a data array? 如何在反应 state 中将元素推入数组,数组位于 object 内部 - How to push elements into array in react state, the array is located inside of object (javascript)是否可以在位于另一个 object 内的阵列中推送 object? - (javascript)Is it possible to push an object in the array located inside another object? 从位于阵列内的 arrays 中删除逗号 - Remove commas from arrays located inside an array 如何从位于数组中的元素创建数组? - How to create an array from elements located in an array? 如何切换位于来自 API 的另一个数组的每个 object 中的数组的显示? - How can I toggle the display of an array that is located within each object of another array that comes from an API? 如何修改和扩展位于 useState 数组中的对象的属性? - How can I modify and extend properties of an object located in useState Array? 如何通过提取 Javascript/Typescript 中另一个对象数组中的数组来创建对象数组? - How can I create an array of objects by extracting an array that is inside another array of objects in Javascript/Typescript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM