Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Associative Array

Hey there,

I'm try to make an associative array but doesn't work, here's the code I'm using:

var data = [];
for (i=0; i<=(gallery.length)-1; i++) {
data[]['title'] = gallery.item(i).getElementsByTagName("title").item(0).text;

Also I try to made data[i][&#39;title&#39;] = inside the for, don't work.

In JS normally I define "data" as Object() to make it work, I try that but the error message it's the same:

[ERROR] Syntax Error = Parse error at app.js (line 65)

Any ideas will be very helpfully, thanks in advanced.

— asked June 17th 2010 by Esteban Fernandez
  • array
  • associative
  • javascript
1 Comment
  • for (i=0; i&lt;=(gallery.length)-1; i++)
    must be :
    for (i=0; i &lt; gallery.length; i++)
    kind regards ;)

    — commented April 4th 2012 by Pierre Lesigne

3 Answers

  • You will need something like this:

    var data = new Array();
    for (i=0; i<=(gallery.length)-1; i++) {
        //create associative entry for data array
        var entry = new Object();
        entry['title'] = gallery.item(i).getElementsByTagName("title").item(0).text;
        //add to data array
    — answered December 1st 2010 by Dieter Wimberger
  • Hi everyone I am working actually on this Gallery
    inline link text
    I am trying to make it work with a XML file I was thinking that my code was ok, but apparently not, I can't display the pictures and I really don't know why. If someone can help me about that will be great. Here is my code.

    var pictureGallery = Titanium.UI.createWindow({
        tabBarHidden: true,
    var locate="http://www.damienbigot.com/others/test/galery/"
    var ImageGallery = require('com.codeboxed.imagegallery');
    // Images array
    var imagesArray = new Array();
    c = Titanium.Network.createHTTPClient();
    var url="http://www.damienbigot.com/others/test/galery/gallery.xml";
    //c.file = Ti.Filesystem.getFile(dirS2.nativePath,"gallery_verif.xml");
    c.onload = function() {
    var arbo = Ti.XML.parseString(this.responseText);
        var mesItem = arbo.getElementsByTagName("mon_item");
        for (var i=0; i < mesItem.length; i++) 
          var item = mesItem.item(i);
           var large_thumbnails = new Object();
        var description_event = new Object();
        large_thumbnails['image_gallery'] = mesItem.item(i).getElementsByTagName("image_gallery").item(0).text;
        description_event['titre_gallery'] = mesItem.item(i).getElementsByTagName("titre_gallery").item(0).text;
        imagesArray.push({filename: locate+large_thumbnails, caption:locate+description_event});
    var imageGallery = ImageGallery.create({images: imagesArray});
    — answered November 29th 2011 by Damien Bigot
  • to add elements to an array in javascript you need the push method like this:

    array.push(element1, element2, …, elementx);

    for your code it would go like this inside the for loop:

    title: gallery.item(i).getElementsByTagName('title').item(0).text

    — answered June 17th 2010 by Christian Sigl
    1 Comment
    • it work, but i still need a key for the associatio, like this:

      data[i][title] = gallery.item(i).getElementsByTagName('title').item(0).text;
      data[i][description] = gallery.item(i).getElementsByTagName('description').item(0).text

      any idea how to do that ? thanks in advanced!

      — commented June 17th 2010 by Esteban Fernandez
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.