Vis*_*ati 2 jquery json traversal
我有以下json数据
[
{
id: "79",
title: "Web+Infographics",
path: "web-infographics"
},
{
id: "80",
title: "Miscellaneous",
path: "miscellaneous"
},
{
id: "81",
title: "Entertainment",
path: "entertainment"
}
]
Run Code Online (Sandbox Code Playgroud)
我想用jquery从中获取id,title和path如何才能做到这一点?提前致谢.
很简单,使用jQuery.each:
$.each(data, function (index, item) {
console.log(item);
});
Run Code Online (Sandbox Code Playgroud)
但是,对于这个简单的任务,你真的不需要jQuery,Array.prototype.forEach试试本机:
data.forEach(function (item) {
console.log(item);
});
Run Code Online (Sandbox Code Playgroud)
如果你必须支持旧的浏览器并且不想依赖于库,那么for-loop可以解决这个问题:
for (var i = 0; i < data.length; ++i) {
var item = data[i];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10169 次 |
| 最近记录: |