简体   繁体   English

Javascript JSON 对象数组到 HTML 表

[英]Javascript JSON array of objects to HTML table

I am new to javascript.我是 javascript 的新手。 I also tried different solutions in the forums.我还在论坛中尝试了不同的解决方案。 I want to print object data into a html table (tr>td).我想将 object 数据打印到 html 表中(tr>td)。 I need to pull this data from a url.我需要从 url 中提取这些数据。 The JSON structure is as given in the example. JSON 结构如示例中所示。 Also, the output of the data needs to be side by side.另外,数据的output需要并排。 I tried with the code block below but it didn't work.I tried the suggested answers in this forum.我尝试了下面的代码块,但没有成功。我在这个论坛上尝试了建议的答案。 All the data is coming, but what I want is to list the information of one person side by side on each line.所有的数据都来了,但我想要的是在每一行并排列出一个人的信息。 How can I solve this?我该如何解决这个问题? Thank you.谢谢你。

Note: 1-I know the answer in this link (It is not duplicate quesion).注意:1-我知道此链接中的答案(这不是重复的问题)。 But as I said I want to sort the data side by side.但正如我所说,我想并排对数据进行排序。 2-I am not creating this data. 2-我没有创建这些数据。 I need to get it from a url like this.我需要像这样从 url 那里得到它。

 { "name": [ "John", "Marie", "Clara" ], "surname": [ "Doe", "Jane", "Smith" ], "phone": [ "1233215555", "1233215555", "1233215555" ], "birthdate": [ "1980-12-14", "1990-02-13", "1995-03-10" ] } fetch(url).then(res => res.json()).then((out) => { for (let i = 0; i < out.length; i++) { console.log(out[i][name] + " " + out[i][surname] + " " + out[i][phone] + " " + out[i][birthdate]) } })

The order is wrong.顺序是错误的。 out is an object of arrays (not an array of objects). out 是 arrays 的 object (不是对象数组)。 Here I fixed the order of access with the assumption, that all properties contain an array with the same length:在这里,我通过假设所有属性都包含一个长度相同的数组来固定访问顺序:

 function fetch() { return Promise.resolve({ json() { return Promise.resolve({ "name": [ "John", "Marie", "Clara" ], "surname": [ "Doe", "Jane", "Smith" ], "phone": [ "1233215555", "1233215555", "1233215555" ], "birthdate": [ "1980-12-14", "1990-02-13", "1995-03-10" ] }) } }); } var url = "some-url"; fetch(url).then(res => res.json()).then((out) => { for (let i = 0; i < out.name.length; i++) { console.log(out.name[i] + " " + out.surname[i] + " " + out.phone[i] + " " + out.birthdate[i]) } })

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM