简体   繁体   English

如何遍历作为对象数组的 JSON 对象?

[英]How to loop through JSON object that is array of objects?

I just need to retrieve an object by its id from this JSON object (peopleData).我只需要从这个 JSON 对象 (peopleData) 中通过它的 id 检索一个对象。 When I do typeof peopleData , it returns object.当我做typeof peopleData ,它返回对象。 When I try to do typeof peopleData[0] (treating this object as an array because it has brackets as the opening and closing elements) it returns undefined .当我尝试执行typeof peopleData[0] (将此对象视为数组,因为它有括号作为开始和结束元素)时,它返回undefined How do I loop through all of the objects to find the appropriate object if this JSON object is an array that returns "undefined" when I try to get a specific element of that array?如果这个 JSON 对象是一个数组,当我尝试获取该数组的特定元素时返回“未定义”,我如何遍历所有对象以找到合适的对象? I don't know how to enumerate over or de-structure this object since it is an array.我不知道如何枚举或解构这个对象,因为它是一个数组。

Here are the first few lines from the JSON that I was given (I changed the values but that is irrelevant. I am mainly emphasizing the bracket at the beginning).这是我收到的 JSON 中的前几行(我更改了值,但这无关紧要。我主要在开头强调括号)。 This is the raw JSON object that I copied from its raw form on GitHub:这是我从 GitHub 上的原始形式复制的原始 JSON 对象:

[{
    "id": "237856238235",
    "ip_address": "3423423.42.42.4",
    "ssn": "123133231",
    "date_of_birth": "3123123",
    "address": {
        "home": {
            "street_number": "231231",
            "street_name": "ef2ef23",
            "street_suffix": "8i6rth2",
            "city": "wefwdfwef"
            "state": "affwefwfww",
            "zip": "wefsdfbghyj"
        },
        "work": {
            "street_number": "wefgwegwe",
            "street_name": "wefwefwf",
            "street_suffix": "wsfaf",
            "city": "aefaef",
            "state": "afaef",
            "zip": "aefaef"
        }
    }
}, {
    "id": "fwefewf",
    "ip_address": "fwefwf",
    "ssn": "wfwef",
    "date_of_birth": "wefwef",
    "address": {
        "home": {
            "street_number": "efwef",

I have done this before with this exact data and it worked when I just used a small function that cycles through each element of the array, but I have no idea why it is not working now.我以前用这个精确的数据完成了这个,当我只使用一个循环遍历数组的每个元素的小函数时它就起作用了,但我不知道为什么它现在不起作用。

I diagnosed my problem incorrectly.我错误地诊断了我的问题。 It did not have to do with the JSON data.它与 JSON 数据无关。 I forgot to use await when getting the data with Axios.await获取数据的时候忘记用await了。 Thanks so much for any effort you put in to help me with this.非常感谢您为帮助我所做的一切努力。

You can use filter by the id you want.您可以按所需的 id 使用过滤器。

 const data = [{ "id": "237856238235", "ip_address": "3423423.42.42.4", "ssn": "123133231", "date_of_birth": "3123123", "address": { "home": { "street_number": "231231", "street_name": "ef2ef23", "street_suffix": "8i6rth2", "city": "wefwdfwef", "state": "affwefwfww", "zip": "wefsdfbghyj" }, "work": { "street_number": "wefgwegwe", "street_name": "wefwefwf", "street_suffix": "wsfaf", "city": "aefaef", "state": "afaef", "zip": "aefaef" } } }, { "id": "fwefewf", "ip_address": "fwefwf", "ssn": "wfwef", "date_of_birth": "wefwef", "address": { "home": { "street_number": "efwef", "street_name": "ef2ef23", "street_suffix": "8i6rth2", "city": "wefwdfwef", "state": "affwefwfww", "zip": "wefsdfbghyj" }, "work": { "street_number": "wefgwegwe", "street_name": "wefwefwf", "street_suffix": "wsfaf", "city": "aefaef", "state": "afaef", "zip": "aefaef" } } }] let result = data.filter(obj => obj.id === "237856238235") //console.log(result) console.log(result[0])

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

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