gpt4 book ai didi

javascript - 通过对象解析

转载 作者:行者123 更新时间:2023-11-30 07:46:10 25 4
gpt4 key购买 nike

我有这个对象,我想获取门户名称。我该怎么做,使用 JS 或 jQuery

对象

var Test = {};

Test.Customers = [{"CustomerItems":
"Portals":
{"id":"1","customer":"1950","resident":"yes",
"CustomPortals":
[
{"id":"11","test":"4251","portalname":"tye.jpg"},
{"id":"12","test":"4251","portalname":"Lsdf.jpg"},
{"id":"13","test":"4251","portalname":"nick.jpg"}
]
}
},
{"CustomerItems":
"Portals":
{"id":"2","customer":"1952","resident":"yes",
"CustomPortals":
[
{"id":"14","test":"4252","portalname":"Chrysanthemum2.jpg"},
{"id":"15","test":"4255","portalname":"navagin.jpg"},
{"id":"16","test":"4257","portalname":"jasoria.jpg"}
]
}
},
{"CustomerItems":
"Portals":
{"id":"3","customer":"1950","resident":"yes",
"CustomPortals":
[
{"id":"17","test":"4231","portalname":"Zsryanmum1.jpg"},
{"id":"18","test":"4651","portalname":"Ltd1.jpg"},
{"id":"19","test":"4281","portalname":"ser1.jpg"}
]
}
}
]

尝试过

 $.each(Test.Customers, function(index, value) {

$.each(value.CustomPortals, function(innerIndex, innerValue) {
alert('File ' + innerValue + ' in customer ' + innerIndex);
});

});

但是没用

最佳答案

您的内部“.each()”循环需要遍历“CustomerItems.Portals”以到达“CustomPortals”:

            $.each(value.CustomerItems.Portals.CustomPortals, function(innerIndex, innerValue) {
alert('File ' + innerValue + ' in customer ' + innerIndex);
});

添加一些存在性测试可能是个好主意,但你比我更了解你的数据。

edit — @justkt 有一个非常好的观点——正如此处发布的那样,JSON 首先是无效的。因此,我上面写的内容将是正确的如果这些东西可以被解析:-)

关于javascript - 通过对象解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5734089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com