Instead of passing an array of arrays, Javascript is passing an array of objects

Having issues passing a Javascript Array() to Flash via FlashVars. Can you assist in identifying the error in my setup?

javascript code

// custom array of user-defined cities
var usercities = new Array( 
    {'name':"London", 'latitude':51.5002, 'longitude':-0.1262 },
    {'name':"NYC", 'latitude':51.5002, 'longitude':-0.1262 } 
);

flashvars.newcities = usercities;

flash code

// pre-populated city array shown when no user data entered
var cities:Array = new Array(
    { name:"London", latitude:51.5002, longitude:-0.1262 },
    { name:"NYC", latitude:40.7144, longitude:-74.0060 }
);

// retrieve FlashVars
var newcities:Object = LoaderInfo(this.root.loaderInfo).parameters.newcities;
if(newcities != null) {
    cities = newcities;
};

The current setup is not working as intended. It's crucial to keep the structure of the cities array on the Flash end unchanged. Modifications can be made to the Javascript side.

Your assistance in resolving this matter is greatly appreciated.

Answer №1

Unlike some programming languages, JavaScript does not support associative arrays. Instead, you can use objects to create named indexes. When an array is assigned a value with a named index, it essentially becomes an object.

If you want to implement this functionality, you may need to make adjustments to your Flash code. As suggested by meder, serializing your array would be the most effective approach. I recommend using JSON encoding in JavaScript and decoding in Flash for optimal results.

Answer №2

If you prefer, you can create arrays manually like this:

var usercities = [];
usercities[0] = [];
usercities[0]["name"] = "London";
usercities[0]["latitude"] = 51.5002;
usercities[0]["longitude"] = -0.1262;
usercities[1] = [];
usercities[1]["name"] = "NYC";
usercities[1]["latitude"] = 40.7128;
usercities[1]["longitude"] = -74.0060;

Even though the structure is the same, Flash might interpret it differently.

Answer №3

Implemented the values in this manner:

Javascript

var cities = new Array( 
    Array("London", 51.5002, -0.1262),
    Array("NYC", 40.7144, -74.0060),
);

The string is retrieved as a flash.

"London",51.5002,-0.1262,"NYC",40.7144,-74.0060

Then I broke down the string and transformed it into an array. It's not the cleanest method, but it gets the job done in the end. Just make sure each row in the array has exactly 3 items and none of them contain commas.

Sharing this in case it proves helpful to someone.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with displaying Vue data from vuex in the user interface

I'm facing a challenge in displaying plan data on my Vue application. The data is fetched from an API running locally. Although I have successfully added the data to the store/vuex and verified its correctness using vue dev tools, I am unable to visua ...

Calculate the number of elements in an array that have numerical keys

I'm working with an interesting array: Array ( [title] => title [weight] => 0 [0] => Text1 [1] => Text2 [additional] => Info } Is there a way to count the elements in this array that have numeric keys? ...

My hosting provider is not allowing the Express server to serve static files

I am facing an issue where my Express.js app is serving my js file locally, but on my hosting platform, I am unable to access the js file. var express = require("express") var app = express() var path = require("path") var bodyParser = ...

Having trouble with jQuery find and attribute selectors?

On my webpage, there is a form where: The command $('form').find('input[type=submit]') returns [undefined] However, using $('form input[type=submit]') works correctly... Is this behavior expected? ...

Creating a Kendo Menu within an Ext JS Panel

Currently, I am experimenting with combining ExtJS and Kendo UI - a unique mix that is taking me off the usual path ;) I have managed to render a Kendo Menu onto an Ext JS (4.2.1) generated Ext.form.Panel In case you want to check it out, here's a F ...

What is the best method for searching through all keys of an object?

Multiple documents contain a key called userId, where this key is always an object: { "id": { "$oid": "22fc6b11a0ff111d598b114f" }, "userId": { "KEY1" : ["..."], "KEY2" : ["..."], ...

Guide to writing a Jasmine test case to verify Toggle class behavior within a click event

My directive is responsible for toggling classes on an element, and it's working as expected. However, I seem to be encountering an issue with the jasmine test case for it. // Code for toggling class fileSearch.directive('toggleClass', func ...

Highcharts: No matter what I try with the formatter function or setting the tooltip to enabled:false, nothing seems to make a difference

series: [ { tooltip:{ enabled:true, formatter:function(){ var $index=this.point.index; return $steps_array[$index]+& ...

Accessing the current position of skeleton bones in Three.js while an animation is playing

I currently have a Three.js project with an animated scene and I am trying to obtain the positions of skeleton bones at various points in time. If I visit, for instance, and inspect the Three.js scene: const modelViewer = document.querySelector("m ...

php split not functioning in javascript template

I have come across some previous discussions on the same topic. Below are the links to those posts: Javascript Template Engine Use with jQuery What is x-tmpl? I am attempting to integrate a php explode function into a javascript template. The plugin I ...

Has the binary search operation not been executed?

My attempt to implement the binary search algorithm in my code example is not producing the expected result. I'm unsure of the cause. Can someone please explain it to me? var array = [1, 4, 6, 8, 9, 12, 15, 17, 19, 34, 55, 78, 80]; function binarySe ...

Utilize the @Attribute feature in creating custom attribute directives and data-binding attribute directives

I am exploring Angular for the first time and I have a question about using @Attribute in attribute directives. The code snippet below is from a book that I am studying: @Directive({ selector: "[pa-attr]", }) export class PaAttrDirective { constructo ...

Jquery animate not working properly after closing the div for the first time

I am currently working with a div that utilizes the .animate function. The CSS for the div is set as follows: position:fixed; bottom:-240px; The associated animate script looks like this: $("#media").click(function () { $("#mediadetails").animate({ ...

Attempting to update using the .put() method proved futile as it has been deprecated. I then attempted to use the updateOne

.put(function(req,res) { console.log(req.body.content); Article.update( {title:req.params.specificarticle}, {title:req.body.title, content:req.body.content}, {overwrite:true}, function(err) { if(!err){ res.send("Article has been updated successfully ...

Encountering an issue while trying to launch an Angular application on localhost. Vendor.js resource failed to load

Whenever I compile the code, it runs successfully. However, when I try to serve the application using 'node --max-old-space-size=8192', even though it compiles without any errors, when I open the app in a browser, it returns an error saying "Cann ...

Filtering an array by a search term

How do you filter an array based on a specific search term? For example, if we have an array [Tom Harry, Tom John, John Glen, Tom Harward] and we search for "Tom H," then only Tom Harry and Tom Harward should be the output. [Tom Harward, Tom Harry]; Usin ...

Encountering Babel issues while incorporating npm package into React Native project

Currently, I am developing a React Native application. In an attempt to enhance my application, I decided to incorporate the npm package available at this link: https://github.com/MagicTheGathering/mtg-sdk-javascript/ Despite trying various import statem ...

Locating a specific element in javascript using its id

I'm struggling to automate the process of downloading a CSV file from a website using Selenium. Specifically, I'm having trouble selecting the format of the file and clicking on export. Does anyone have any ideas on how to accomplish this? To acc ...

Using d3 in a Node project can lead to a jsdom error occurring

I'm feeling really lost with the current situation. When I try to use d3 version 2.10.1, the require 'd3' line triggers an error related to jsdom in the index.js file of d3: document = require("jsdom").jsdom("<html><head></he ...

JavaScript failing to load specifically on Rails Spree Commerce site

Currently, I am experimenting with Spree commerce to improve my understanding of it. I have obtained an HTML theme () and my intention is to use it as the front-end design of the site, not for the actual store itself. The store will be accessed through /s ...