0

I am trying to set up an array of keys that are strings instead of numbers. But, when I try to do so, the array ends up null.

Here is the function that works (where the keys are just a simple number "i":

function loadImages(arr, data, callBack){
                var count = 0;
                var img = new Array();

                for(var i in arr ){
                    var src = "\""+arr[i]+"\"";

                    img[i] = new Image();
                    img[i].src = arr[i];
                    img[i].onload = function(){
                        count++;
                        if(count == arr.length){
                            callBack(data, img);
                        }
                    }
                }
            } 

Here is the function I am attempting to use but the resulting array is null:

function loadImages(arr, data, callBack){
                var count = 0;
                var img = new Array();

                for(var i in arr ){
                    var src = "\""+arr[i]+"\"";

                    img[src] = new Image();
                    img[src].src = arr[i];
                    img[src].onload = function(){
                        count++;
                        if(count == arr.length){
                            callBack(data, img);
                        }
                    }
                }
            } 

I have tried defining "src" in the following ways too:

var src = arr[i];

var src = "'"+arr[i]+"'";

Does anyone know why it is resulting in null?

3
  • 1
    You need an object {}. Arrays indices are numeric. Commented Nov 13, 2013 at 7:15
  • Of course! Thank you so much elclanrs. A very simple answer. Commented Nov 13, 2013 at 7:16
  • 2
    "\""+arr[i]+"\"" looks very wrong. This will create a string which literally contains quotation marks ("). Commented Nov 13, 2013 at 7:16

2 Answers 2

1

Javascript arrays are not good to be used for an enumerated array. That is what you are trying to do here. Use Object instead.

Then you can use a string as a key.

function loadImages( arr, data, callBack )
{
  var nCount = 0 ;
  var oImg = new Object() ;

  for ( i = 0; i < arr.lenght; i++ )
  {
    var sSrc = "\"" +arr[ i ]+ "\"" ;

    oImg[ sSrc ] = new Image() ;
    oImg[ sSrc ].src = arr[ i ] ;
    oImg[ sSrc ].onload = function()
                          {
                            count++;
                            if ( count == arr.length )
                            {
                              callBack( data, oImg ) ;
                              alert( oImg ) ;
                            }
                          }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

JAVASCRIPT

function loadImages(arr, data, callBack){
                var count = 0;
                var img = new Array();
                for(i=0; i<arr.lenght; i++ ){
                    var src = "\"" +arr[i]+ "\"";

                    img[src] = new Image();
                    img[src].src = arr[i];
                    img[src].onload = function(){
                        count++;
                        if(count == arr.length){
                            callBack(data, img);
                            alert(img);
                        }
                    }
                }
            }

Try this..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.