gpt4 book ai didi

javascript - 使用 JQuery 解析 JSON 嵌套数组

转载 作者:行者123 更新时间:2023-11-29 16:10:34 26 4
gpt4 key购买 nike

我有一个 API 可以将以下 JSON 值作为字符串返回。

"[
["West Baton Rouge test hello world", "1"],
["LSU Parking \u0026 Transportation Services", "2"],
["demokljafsk", "3"],
["latest", "19"],
["Hello check", "20"],
["Dinesh Devkota", "21"],
["World", "22"],
["altered value.", "23"],
["Dinesh Devkota", "24"],
["Dinesh Devkota", "25"],
["Dinesh Devkota", "26"],
["Dinesh Devkota", "27"],
["Dinesh Devkota", "28"],
["Rocking Client", "29"],
["West Baton Rouge", "30"],
["Test Client", "31"]
]"

我很难尝试使用 JQuery 获取每个数组的第一个值并使用以下代码将其登录到控制台。

  $.get("/controller", function (data) {
console.log("Data Loaded: " + data);
for (var eachdata in data) {
console.log(eachdata[0]);
}
});

我是 JQUERY 的新手,不知道什么是正确的方法。

最佳答案

不要使用 for..in对于数组

var data = [
["West Baton Rouge test hello world", "1"],
["LSU Parking \u0026 Transportation Services", "2"],
["demokljafsk", "3"],
["latest", "19"],
["Hello check", "20"],
["Dinesh Devkota", "21"],
["World", "22"],
["altered value.", "23"],
["Dinesh Devkota", "24"],
["Dinesh Devkota", "25"],
["Dinesh Devkota", "26"],
["Dinesh Devkota", "27"],
["Dinesh Devkota", "28"],
["Rocking Client", "29"],
["West Baton Rouge", "30"],
["Test Client", "31"]
];

for (var i = 0, len = data.length; i < len; i++) {
console.log(data[i][0]);
}

// with jQuery
$.each(data, function (index, value) {
console.log(value[0]);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

关于javascript - 使用 JQuery 解析 JSON 嵌套数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29546526/

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