아약스를 사용하여 서버를 호출하고 일부 사용자 데이터를 가져 왔습니다. 서버는 다음과 같은 형식의 배열을 다시 반환 : 나는 다음 foreach 루프jquery를 사용하여 다차원 배열의 데이터에 액세스하는 방법?
$.ajax({url: "/url/to/server", success: function(result){
result.forEach(function(item) {
console.log(item['chart_data']['data']);
});
}, complete: function() {
// Schedule the next request when the current one's complete
setTimeout(checkForUpdates, 3000);
}
});
});
를 사용하여 어레이 내의 데이터에 액세스하려고
array:6 [▼
0 => array:17 [▼
"id" => 334
"provider" => "some_provider"
"option_id" => "x124223"
"option_title" => "title"
"api_parameter" => "parameter"
"chart_title" => "title"
"chart_color" => "#6a76fc"
"chart_target" => "2220"
"chart_type" => "line"
"chart_size" => "4"
"chart_data" => array:7 [▼
239 => array:2 [▼
"data" => 2114
"created_at" => "14/August"
]
240 => array:2 [▼
"data" => 2114
"created_at" => "15/August"
]
241 => array:2 [▶]
242 => array:2 [▶]
243 => array:2 [▶]
244 => array:2 [▶]
245 => array:2 [▶]
]
"average" => 2122.0
"current" => array:2 [▶]
"last" => array:2 [▶]
"current_status_icon" => "md-trending-neutral"
"current_status_color" => "#3DDCF7"
"status_message" => "hmm... The needle didnt move."
]
1 => array:17 [▼
"id" => 345
"provider" => "some_other_provider"
"option_id" => "x124"
"option_title" => "Title"
"api_parameter" => "parameter"
"chart_title" => "title"
"chart_color" => "#6A76FC"
"chart_target" => "Set a target"
"chart_type" => "line"
"chart_size" => "4"
"chart_data" => array:7 [▼
202 => array:2 [▼
"data" => 5
"created_at" => "13/August"
]
203 => array:2 [▼
"data" => 5
"created_at" => "14/August"
]
204 => array:2 [▶]
205 => array:2 [▶]
206 => array:2 [▶]
207 => array:2 [▶]
208 => array:2 [▶]
]
"average" => 5.0
"current" => array:2 [▼
"data" => 5
"created_at" => "16/August"
]
"last" => array:2 [▼
"data" => 5
"created_at" => "16/August"
]
"current_status_icon" => "md-trending-neutral"
"current_status_color" => "#3DDCF7"
"status_message" => "hmm... The needle didnt move."
]
을하지만 이것은 단지 undefined
를 기록합니다. 각 최상위 배열 내에서 중첩 된 chart_data에 어떻게 액세스합니까?
당신은 중첩 된 배열에 접근 할 필요가 :'chart_data'는'data' 속성이 요소가있는 배열입니다
는하지만 내 생각 엔이 일 것입니다. 따라서 다른 중첩 루프가 필요합니다. – trincot
@trincot 감사합니다. 'Uncaught TypeError : item.chart_data.forEach가 함수가 아닙니다 '라는 오류가 있습니다. – kevinabraham
JSON 출력은 어디서 오는가? PHP? 구문이 올바르지 않습니다. 질문에'console.log (JSON.stringify (result))'를 할 때 얻는 결과를 포함한다면 유용 할 것이다. 그렇게하면 올바른 JSON이 표시됩니다. – trincot