简体   繁体   English

如何遍历对象数组并访问每个数组内的属性以显示在 html 元素中?

[英]how to loop through an array of objects and access the properties inside each array to display in an html element?

I have a JSON response shown below:我有如下所示的 JSON 响应:

rows: Array(2)
0:
ID: "35"
datedCreated: "2019-10-19 22:34:34"
text: "rferfrefrf"
userid: "1"
__proto__: Object
1:
ID: "36"
datedCreated: "2019-10-20 04:50:45"
text: "fesfsefsfdsdfsdfsdfdsfdsfsdfsdf sdfsdfsdfs"
userid: "2"
__proto__: Object
length: 2
__proto__: Array(0)
__proto__: Object

What I am trying to do is access the data within each object and have it displayed as a an html element like a div.我要做的是访问每个 object 中的数据,并将其显示为 html 元素,如 div。 In this case I would have 2 divs displayed on my html form.在这种情况下,我的 html 表单上会显示 2 个 div。 However if i returned 15 object I would like 15 divs each the different information of the objects.但是,如果我返回 15 object 我想要 15 个 div 每个对象的不同信息。 I know I am supposed to loop through the objects and I know I can do that using with the index but if I want the information divs to display automatically I think I would need to do a nested loop?我知道我应该遍历对象,并且我知道我可以使用索引来做到这一点,但是如果我希望信息 div 自动显示,我认为我需要做一个嵌套循环? One that loops through the number of objects and then another one where I create the div element and supply it with the information from the object.. right?一个循环遍历对象的数量,然后另一个循环创建 div 元素并为其提供来自 object 的信息。对吗? so far I have this.到目前为止,我有这个。

for (var i = 0; i < data.rows.length; i++) {
for (var key in data.rows[i]) {
    for (var j = 0; j < data.rows[i][key].length; j++) {
        console.log(data.rows[i][key][j])
    }
}

} }

And I get the first object我得到了第一个 object

3
5
2
0
1
 9
 -
1
0
-
1
9
 ---- 
2
:
3
4
:
3
4
1
r
f

but I know I'm not doing it correctly.但我知道我做得不对。 but essentially i would need this concept where it shows me the values for each object in the array.但基本上我需要这个概念,它向我显示数组中每个 object 的值。 I know how to create the div i am just struggling with this我知道如何创建我正在为此苦苦挣扎的 div

you can use 'recursion' to achieve your goal.您可以使用“递归”来实现您的目标。 hopefully the link bellow can help you希望下面的链接可以帮助你

looping through an object (tree) recursively 递归循环通过 object(树)

and try to run snippet, hopefully in can help you in some ways.并尝试运行代码片段,希望能在某些方面对您有所帮助。

 var obj = { a1: '', a2: { b1: '', b2: { c1: '', c2: { d1: '', }, c3: { d2: '', } }, b3: '', b4: '', b5: '', }, a3: '' } function eachRecursive(obj, text, dash) { temp = '' for (var k in obj) { temp += '<div>' + ('-'.repeat(dash)) + ' ' + k + '</div>'; if (typeof obj[k] == "object" && obj[k];== null) { dash1 = dash + 1, temp += eachRecursive(obj[k], text; dash1). } } return text + temp } document,write(eachRecursive(obj, ''; 1));

Maybe something like this:也许是这样的:

response.rows.forEach(r => {
    const d = document.createElement("div");

    d.innerText = r.text;
    document.body.appendChild(d);
});

Can't be more specific without knowing what's going in each div exactly, or where they would be displayed on the page, but this should give you a good launching off point.如果不知道每个div中究竟发生了什么,或者它们将在页面上显示的位置,就无法更具体,但这应该会给您一个很好的启动点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在React组件中遍历数组内部对象的属性? - How to loop through properties of objects inside of an array within a React component? 如何遍历包含对象的数组并访问它们的属性 - How to loop through an array containing objects and access their properties 循环遍历对象数组并显示在 HTML - Loop through an array of objects and display in HTML 遍历每个元素并将值添加到新对象。 将这些对象添加到数组并访问其值 - Loop through each element and add values to a new object. Add these objects to array & access their values 如何通过数组与对象内部循环 - How to loop through array with objects inside 如何遍历具有不同属性的100个对象的数组并从每个对象中检索1个属性值? - How to loop through an array of 100 objects with different properties and retrieve 1 property value from each? 如何在循环中显示数组元素 - how to display an element of array inside a loop 遍历javascript数组并在JQuery对话框中显示每个元素 - Loop through javascript array and display each element in JQuery Dialog 遍历对象数组并将每个元素插入html - Loop through object array and insert each element into html 如何遍历每个元素上调用 jquery.each 的数组? - How to loop through an array calling jquery.each on each element?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM