Доступ к данным из многомерных массивов -Ajax Response - PullRequest
0 голосов
/ 20 декабря 2018

Мне нужна помощь в доступе к данным из массива json ответа ajax.

Внутренний 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 голосов
/ 20 декабря 2018

Я думаю, вы хотите что-то подобное в вашем 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)
}))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...