英文:
JavaScript - Convert JSON Array into HTML elements
问题
Here's the translated code part:
我有一个消息的 JSON 数组,需要将其转换为 HTML 元素以在我的网页上显示。我收到的 JSON 如下所示:
{
"data": [
{
"fieldData": {
"Message": "Message 1",
"Timestamp": "04/14/2023 11:39:13"
}
},
{
"fieldData": {
"Message": "Message 2",
"Timestamp": "04/13/2023 10:02:18"
}
},
{
"fieldData": {
"Message": "Message 3",
"Timestamp": "08/19/2021 19:51:21"
}
}
]
}
我需要从数组中提取每个 'Message' 和 'Timestamp' 值,并将它们放在类似以下的标签中,用于第一个数组:
<p class="from-them">Message 1</p>
<time class="timestamp-to">04/14/2023 11:39:13</time>
因此,使用上面的示例 JSON,最终结果将是:
<p class="from-them">Message 1</p>
<time class="timestamp-to">04/14/2023 11:39:13</time>
<p class="from-them">Message 2</p>
<time class="timestamp-to">04/13/2023 10:02:18</time>
<p class="from-them">Message 3</p>
<time class="timestamp-to">08/19/2021 19:51:21</time>
这是我第一次尝试编写 JavaScript 函数将数组转换为单独的 HTML 元素,不确定从何处开始。
Please note that I've only provided the translation of the code part as requested. If you have any questions or need further assistance with this JavaScript task, please feel free to ask.
英文:
I have a JSON Array of messages that I need to convert into html elements to use for displaying in my web page. The JSON that I'm receiving looks like this:
{
"data": [
{
"fieldData": {
"Message": "Message 1",
"Timestamp": "04/14/2023 11:39:13"
}
},
{
"fieldData": {
"Message": "Message 2",
"Timestamp": "04/13/2023 10:02:18"
}
},
{
"fieldData": {
"Message": "Message 3",
"Timestamp": "08/19/2021 19:51:21"
}
}
]
}
I need to extract each 'Message' and 'Timestamp' Value from the Array and place them in tags like the following for the first array:
<p class="from-them">Message 1</p>
<time class="timestamp-to">04/14/2023 11:39:13</time>
So using the sample JSON above the end result would be:
<p class="from-them">Message 1</p>
<time class="timestamp-to">04/14/2023 11:39:13</time>
<p class="from-them">Message 2</p>
<time class="timestamp-to">04/13/2023 10:02:18</time>
<p class="from-them">Message 3</p>
<time class="timestamp-to">08/19/2021 19:51:21</time>
It's my first time trying to write a JavaScript function to convert an array into individual html elements and not sure where to start here.
答案1
得分: 0
- 使用
map()
函数或简单地使用JSON.parse()
将您的JSON转换为JavaScript可以使用的普通对象。 - 然后使用一个for循环遍历数组中的每个对象。
- 使用
createElements
来实际创建该元素。
let json_data = `{
"data": [
{
"fieldData": {
"Message": "Message 1",
"Timestamp": "04/14/2023 11:39:13"
}
},
{
"fieldData": {
"Message": "Message 2",
"Timestamp": "04/13/2023 10:02:18"
}
},
{
"fieldData": {
"Message": "Message 3",
"Timestamp": "08/19/2021 19:51:21"
}
}
]
}`;
// 解析JSON
let data = JSON.parse(json_data);
// 循环创建元素
for (let messageIndex = 0; messageIndex < data.data.length; messageIndex++) {
let message = data.data[messageIndex].fieldData.Message;
let timestamp = data.data[messageIndex].fieldData.Timestamp;
// 创建消息元素
let messageElement = document.createElement('p');
messageElement.classList.add('from-them');
messageElement.textContent = message;
document.body.appendChild(messageElement);
// 创建时间戳元素
let timestampElement = document.createElement('time');
timestampElement.classList.add('timestamp-to');
timestampElement.textContent = timestamp;
document.body.appendChild(timestampElement);
}
英文:
- convert your JSON in a normal Object that JS can use either by using the
map()
function or by simply parsing the JSON withJSON.parse()
- then use a for-loop that loops through every object within the array
- Use
createElements
to actually create that element:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let json_data = `{
"data": [
{
"fieldData": {
"Message": "Message 1",
"Timestamp": "04/14/2023 11:39:13"
}
},
{
"fieldData": {
"Message": "Message 2",
"Timestamp": "04/13/2023 10:02:18"
}
},
{
"fieldData": {
"Message": "Message 3",
"Timestamp": "08/19/2021 19:51:21"
}
}
]
}`;
// parse JSON
let data = JSON.parse(json_data);
// loop to create the elements
for (let messageIndex = 0; messageIndex < data.data.length; messageIndex++) {
let message = data.data[messageIndex].fieldData.Message;
let timestamp = data.data[messageIndex].fieldData.Timestamp;
// create Message
let messageElement = document.createElement('p');
messageElement.classList.add('from-them');
messageElement.textContent = message;
document.body.appendChild(messageElement);
// create Timestamp
let timestampElement = document.createElement('time');
timestampElement.classList.add('timestamp-to');
timestampElement.textContent = timestamp;
document.body.appendChild(timestampElement);
}
<!-- end snippet -->
答案2
得分: -1
创建一个HTML元素,需要做类似以下的操作:
var pElement = document.createElement('p'); //使用不同参数以创建不同元素
pElement.className = 'className';
pElement.textContent = 'textContent';
// 最后将其放入DOM中
document.body.appendChild(pElement);
现在,只需创建一个for循环(对于"data"),在每次迭代中创建p元素和time元素,然后将它们附加到DOM中。
英文:
To make a html element, you need to do something like:
var pElement = document.createElement('p'); //use different parameter to make different elements
pElement.className = 'className';
pElement.textContent = 'textContent';
// and essentially you put it in the DOM
document.body.appendChild(pElement);
Now, you just need to make a for loop (for "data") and within each iteration make the p element and time element, and append them to the DOM.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论