Aso Strife Aso Strife - 6 months ago 8
Ajax Question

Multiple arrays in javascript doens't work passing values

I'm trying to create a multidimensional arrays in JS like this:

On my app.js file I have this

var equipValue = new Array();
var selectedTrucks = new Array();


In my function.js file instead I have:

for (var i in selectedTrucks) {
(function(i){
// Simple Ajax request that return me a Json values
var ajaxUrl = '/ajaxRequest/getSingleTruckPosition/' + selectedTrucks[i];

$.ajax({
url: ajaxUrl,
})
.done(function(data) {
if(data != null){
// equipValue doens't go.
// Unable to set property '0' of undefined or null reference
equipValue[i] = jQuery.parseJSON(data);
}
});


Why I obtain: Unable to set property '0' of undefined or null reference ?
Why that variables doens't work on my function.js file but selectedTrucks work perfectly?

Thanks guy

EDIT

Doing some test I've tried this:

equipValue = jQuery.parseJSON(data);


So, the problem isn't scoope but create multidimensional array on Runtime? Or similar?

** EDIT 2**

Here the problem:

var selectedTrucks = new Array();
// other 15 lines of codes and then this:
if(selectedTrucks === undefined || selectedTrucks.length == 0){
cleanMapMarkers();
}


Why cleanMapMakers() is always launched even when selectedTrucks is empty?? All problems that I've found is here.
Thanks

Answer
  • Update : seems equipValue is changed, or overwritten to a different type, somewhere else.

In your code, maybe you're looking for, if selectedTrucks is undefined OR if it's not empty

if(selectedTrucks === undefined || selectedTrucks.length != 0){
        cleanMapMarkers();
    }

  • jQuery.parseJSON() parses json text to an object, but the callback already returns an object

.done(function(data) {
         if (data != null) {
           equipValue[i] = data;

http://jsfiddle.net/dw74gk7j/