简体   繁体   English

Vue.Js:如何在JSON中遍历子对象

[英]Vue.Js : How to loop through child objects in JSON

I am trying to create a simple email list in a table from a json file. 我正在尝试从json文件的表中创建一个简单的电子邮件列表。 Here is the first two bits of the JSON email data: 这是JSON电子邮件数据的前两位:

    [{
    "from": "tevery0@howstuffworks.com",
    "to": ["mdonisthorpe0@google.cn", "efinker1@chron.com"],
    "subject": "Expanded modular website",
    "attachment": [{
        "filename": "dummyfile.pdf",
        "location": "https://fakelink.com"
        },
        {
        "filename": "dummyfile_two.pdf",
        "location": "https://fakelink.com"
        }
    ],
    "date": "2017/09/18",
    "body": "Morbi non lectus. Aliquam sit amet diam in magna bibendum imperdiet. Nullam orci pede, venenatis non, sodales sed, tincidunt eu, felis.\n\nFusce posuere felis sed lacus. Morbi sem mauris, laoreet ut, rhoncus aliquet, pulvinar sed, nisl. Nunc rhoncus dui vel sem."
    }, {
    "from": "efinker1@chron.com",
    "to": "ntregensoe1@bluehost.com",
    "subject": "Optional tertiary task-force",
    "attachment": [{
        "filename": "dummyfile.pdf",
        "location": "https://fakelink.com"
        },
        {
        "filename": "dummyfile_two.pdf",
        "location": "https://fakelink.com"
        }
    ],
    "date": "2019/03/25",
    "body": "Sed ante. Vivamus tortor. Duis mattis egestas metus.\n\nAenean fermentum. Donec ut mauris eget massa tempor convallis. Nulla neque libero, convallis eget, eleifend luctus, ultricies eu, nibh.\n\nQuisque id justo sit amet sapien dignissim vestibulum. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Nulla dapibus dolor vel est. Donec odio justo, sollicitudin ut, suscipit a, feugiat et, eros."
    }]

This is an imported JSON from a file, and I access it in my component like so: 这是从文件导入的JSON,我可以像这样在组件中访问它:

<script>
import messages from '../assets/dummy_email_data.json'

export default {
 data() {
  return {
    messages: messages,
 };
},
</script>

and then to get them in a table, I am looping through it like so: 然后将它们放在表中,我像这样遍历它:

<tbody>
    <tr v-for="(data, index) in messages" :key="index">
        <td>{{ data.from }}</td>
        <td>{{ data.to }}</td>
        <td>{{ data.subject }}</td>
        <td>{{ data.date }}</td>
    </tr>
</tbody>

For the most part it works fine, however, when I get to the to field on the first object I get this output: 在大多数情况下,它运行良好,但是,当我到达第一个对象的to字段时,将得到以下输出:

在此处输入图片说明

What I want to do is to be able to loop through that array, count it and display it like so: 我想要做的是能够遍历该数组,对其进行计数并像这样显示它:

在此处输入图片说明

For some reason it seems be be outputting that as a string. 由于某种原因,它似乎是以字符串形式输出的。

How do I get it to also loop through that child object so I can perform actions on it, like count etc... 我如何获取它还可以遍历该子对象,以便可以对其执行操作,例如count等。

Edit: when I try and do a simple .length : 编辑:当我尝试做一个简单的.length

{{ data.to.length }}

I get: 我得到:

在此处输入图片说明

The first one appears to be right, well I would expect 2 . 第一个似乎是正确的,我很希望2 but I would not expect 24 for the second one as there is only 1 item. 但我不希望第二个有24个,因为只有1个项目。

Ah.. The issue was: 嗯..问题是:

  • My second to item was not in an array: "to": "ntregensoe1@bluehost.com" . 我的第二项不在数组中: "to": "ntregensoe1@bluehost.com" So when I was getting the value of 24 I was just assuming the code was not working. 因此,当我获得24的值时,我只是假设代码无法正常工作。

When I fix that in the JSON I can then use: 当我在JSON中修复该错误时,可以使用:

<td>{{ data.to[0]}} <span v-if="data.to.length > 1">+ {{ data.to.length - 1 }}</span></td>

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

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