从多维数组访问数据-Ajax响应

时间:2018-12-20 19:22:20

标签: javascript arrays ajax multidimensional-array response

我需要从ajax响应json数组访问数据的帮助。

后端PHP脚本返回的数组如下:

[
    [
        {
            AccessID: "178638920205",
            ActionDate: "2018-12-18 01:36:56",
            ActionDescription: "Richard Mahdi logged out at 2018-12-18 01:36:56 AM.",
            ActionType: "Log Out",
            Record_Number: "6890701492",
            ResponseLevel: "0",
            custNames: "Richard Mahdi",
            id: "9",
            thumbNameClass: "qRichardS",
            useThumbNail: "https://subdomain.domain.com/adminodocs2019001/richard686827.png"
        }
    ],
    [
        {
            AccessID: "178638920205",
            ActionDate: "2018-12-18 01:35:32",
            ActionDescription: "Richard Mahdi sent Feed Back. Feedback Type: Request. If Feedback is a complaint, you might want to address that immediately via direct communication. View Feedback message from Feedback Section",
            ActionType: "Feed Back",
            Record_Number: "6890701492",
            ResponseLevel: "5",
            custNames: "Richard Mahdi",
            id: "8",
            thumbNameClass: "gRichardB",
            useThumbNail: "https://subdomain.domain.com/adminodocs2019001/richard686827.png"
        }
    ],
    [
        {
            AccessID: "178638920205",
            ActionDate: "2018-12-18 01:33:25",
            ActionDescription: "Richard Mahdi sent Feed Back. Feedback Type: Complaints. If Feedback is a complaint, you might want to address that immediately via direct communication. View Feedback message from Feedback Section",
            ActionType: "Feed Back",
            Record_Number: "6890701492",
            ResponseLevel: "5",
            custNames: "Richard Mahdi",
            id: "7",
            thumbNameClass: "fRichardU",
            useThumbNail: "https://subdomain.domain.com/adminodocs2019001/richard686827.png"
        }
    ],
    [
        {
            AccessID: "178638920205",
            ActionDate: "2018-12-18 01:32:27",
            ActionDescription: "Richard Mahdi successfully completed Initial Account Setup. No action required.",
            ActionType: "Initial Account Setup",
            Record_Number: "6890701492",
            ResponseLevel: "0",
            custNames: "Richard Mahdi",
            id: "6",
            thumbNameClass: "tRichardD",
            useThumbNail: "https://subdomain.domain.com/adminodocs2019001/richard686827.png"
        }
    ]
]

尝试了几种处理数据和访问数组元素的方式。目前停留在:

var datatosend = { 'adminoID': "12345"};
 
$.ajax({
    url: "dtrack.php",
    data: datatosend,
    type: 'POST',
    contentType: false,
    processData: false,
    dataType: "json",
    cache: false,
    success: function(response){
        $.each(response,function(x,y){
            console.log(y.ActionDescription);
            alert(y.ActionDescription);
        });	 
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

感谢所有建议或解决方案。

1 个答案:

答案 0 :(得分:0)

我认为您需要success处理程序中类似的内容:

response.forEach(x => x.forEach(y => {
  console.log(y.ActionDescription)
}))

您显然可以使用jQuery#each做类似的事情,但是没有理由在现代网络上使用它。 Array.prototype.forEach很好。

该语言没有真正的多维数组。数组的数组尽可能接近。但是,如果内部数组中每个数组始终只有一个元素,我强烈建议您将它们展平,因为它们非常没用。

const response = [        
    [{"id":"9","ActionDate":"2018-12-18 01:36:56","custNames":"Richard Mahdi","Record_Number":"6890701492","AccessID":"178638920205","ActionType":"Log Out","ActionDescription":"Richard Mahdi logged out at 2018-12-18 01:36:56 AM.","ResponseLevel":"0","thumbNameClass":"qRichardS","useThumbNail":"https:\/\/subdomain.domain.com\/adminodocs2019001\/richard686827.png"}],
    [{"id":"8","ActionDate":"2018-12-18 01:35:32","custNames":"Richard Mahdi","Record_Number":"6890701492","AccessID":"178638920205","ActionType":"Feed Back","ActionDescription":"Richard Mahdi sent Feed Back. Feedback Type: Request. If Feedback is a complaint, you might want to address that immediately via direct communication. View Feedback message from Feedback Section","ResponseLevel":"5","thumbNameClass":"gRichardB","useThumbNail":"https:\/\/subdomain.domain.com\/adminodocs2019001\/richard686827.png"}],
    [{"id":"7","ActionDate":"2018-12-18 01:33:25","custNames":"Richard Mahdi","Record_Number":"6890701492","AccessID":"178638920205","ActionType":"Feed Back","ActionDescription":"Richard Mahdi sent Feed Back. Feedback Type: Complaints. If Feedback is a complaint, you might want to address that immediately via direct communication. View Feedback message from Feedback Section","ResponseLevel":"5","thumbNameClass":"fRichardU","useThumbNail":"https:\/\/subdomain.domain.com\/adminodocs2019001\/richard686827.png"}],
    [{"id":"6","ActionDate":"2018-12-18 01:32:27","custNames":"Richard Mahdi","Record_Number":"6890701492","AccessID":"178638920205","ActionType":"Initial Account Setup","ActionDescription":"Richard Mahdi successfully completed Initial Account Setup. No action required.","ResponseLevel":"0","thumbNameClass":"tRichardD","useThumbNail":"https:\/\/subdomain.domain.com\/adminodocs2019001\/richard686827.png"}]
]

response.forEach(x => x.forEach(y => {
  console.log(y.ActionDescription)
}))