Skip to main content

My associative array doesnt store objects



It seems it's always empty :







var idStruttura=2;

var arrayMarkers=new Array();



arrayMarkers["sede_"+idStruttura] = "ciao";

alert(arrayMarkers.length);







Prints always 0. Why? And how can I fix it?


Comments

  1. An array is not an associative array, however the array object is an object, and all objects are associative arrays. If you use a string as key when assigning items to the array, you are not using it as an array, you are using it as an object.

    The length property of the array returns how many items you have stored in the array. If you also use the array object as an associative array, that won't affect how you use the array as an array.

    ReplyDelete
  2. There is no length when you store objects, only when you use the array as intended.

    Try this (DEMO)

    var idStruttura=2;
    var arrayMarkers={}; // creates a more appropriate object than []

    arrayMarkers["sede_"+idStruttura] = "ciao";
    arrayMarkers["sede_"+(++idStruttura)] = "espresso";
    var len = 0;
    for (var o in arrayMarkers) {
    if (arrayMarkers.hasOwnProperty(o)) len++;
    }
    arrayMarkers.length=len
    alert(arrayMarkers.length)

    ReplyDelete
  3. What you've created is a regular array object and added a property to it called sede_.... JavaScript doesn't use associative arrays in the same way a language like PHP does. Arrays are objects that can have properties, but those properties are not among the numerically indexed array elements.

    var idStruttura=2;
    var arrayMarkers=new Array();

    // Push an object onto the array having one property:
    arrayMarkers.push({"sede_" + idStruttura : "ciao"});

    // Or declare it as an object to begin with:
    // This makes more sense....
    var objMarkers = {};
    objMarkers['sede_' + id] = 'ciao';

    ReplyDelete
  4. array's in javascript (unlike php) cannot have string key's, only numeric keys. If you want a string literal as a key, please use objects

    ReplyDelete
  5. What you need is a simple Object, not an Array. Arrays have numeric indexes.

    var markers = {};
    markers['sede_' + id] = 'ciao';


    Also note that {} is the same as new Object() and [] is the same as new Array(). Always use the former ones. It's much clear to any JS developer.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.