私がやっている間違いを理解するのを手伝ってくれる人はいますか? または、同じ問題の別の解決策を教えてください。
http://jsfiddle.net/mrrajesh1982/eynpu8rj/2/
function isValidImageUrl(url, callback) {
$('<img>', {
src: url,
load: function() {
callback(true);
},
error: function() {
callback(false);
}
});
}
var ndata = [{imgUrl: "https://library.barnard.edu/sites/default/files/paired_t_test_output_1_and_2.png"},{imgUrl: "url1"},{imgUrl: "http://statistics-help-for-students.com/How_do_I_report_paired_samples_T_test_data_in_APA_style_files/image002.jpg"}];
for (var i = 0; i < ndata.length; i++){
var imageUrl = ndata[i].imgUrl;
(function(pictureUrl){
isValidImageUrl(pictureUrl, function(result) {
if (!result) {
ndata[i].imgUrl = "http://media.tcc.fl.edu/webcourses/ctll/Developing_Your_Teaching_Philosophy/examples.jpg";
//I am getting error on this line as TypeError: ndata[i] is undefined
//My question here is to how to update the ndata array obejct with valid image URL?
}
});
}(imageUrl));
}
console.log(JSON.stringify(ndata));// Expecting ndata[1].imgUrl should get updated inside self invoking function with valid image url