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]['title'] =
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.
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 data.push(entry); }
-
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.Titanium.UI.setBackgroundColor('#000'); var pictureGallery = Titanium.UI.createWindow({ title:'Photos', barColor:'#000', backgroundColor:'#fff', 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.open('GET',url,false); //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}); pictureGallery.add(imageGallery); pictureGallery.open(); } c.send();
-
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:
data.push({
title: gallery.item(i).getElementsByTagName('title').item(0).text
});