Is it allowed to use numbers as keys in object literal? - actionscript-3

The following examples can be compiled, but I'm not sure if it's documented anywhere:
var o:Object = { 1: 2, 3: 4 };
Can I safely use this in my code?
P.S. I know that I can just use Arrays instead of objects like this one, but sometimes { key: value } is clearer than array initialization.

Yes, sure, you can. The use case for example is {id:valueObject} hash map. Key here automatically converts to String.

Related

Feathers.js mongoose querying

I'm very new to feathers.js, how would you accomplish querying for an object?
{
...,
obj: {
foo: 1,
bar: 1
},
...
}
The following seems to not work
/some-doc?obj['foo']['$eq']=1
Also, how would you tackle a query like checking a size of array
/some-doc?someArray['length']['$gt']=0
I've been trying to send param like
checkArray=true
Process it at before:find but no luck. Is this the right approach?
Thank you,
In general, most queries supported by Mongoose and MongoDB will work for your Feathers service. MongoDB queries on nested fields using the dot notation so it would be:
/some-doc?obj.foo=1
Length queries can be done with the $size operator. To check if an array has a certain length, you can use the dot notation to see if the entry at the index exists (see this answer):
/some-doc?someArray.1[$exists]=true

How do I pass data between tvml views?

I need to pass data between views in my client-server app. For simple string value, I can put them as attributes on the target element and read the value when the select event is triggered on it. From there, I can pass this string value onto the next document pretty easily.
But the problem comes with much more complex data that's in JSON format. I tried doing JSON.stringify(myData) and putting this value in an attribute. But the compiler doesn't like the { in this attribute value.
I could probably try escaping all the different characters that the compiler has problems with. But I don't think that's a good idea.
Is there any way of implementing jQuery's .data() functionality in TVML and TVJS ? Or is there any other way that makes sending data between views a possibility ?
You can pass your data as URL parameters. Then in the new view, get them using Javascript.
EDIT: And I see in the comment above you came to a similar conclusion.
You could keep your data in a semi-global associative array. Store the key in an attribute on the element and use that to get your data structure.
Ex:
var globalData;
function onSelect(e){
var id=e.target.getAttribute("id");
var specificData=globalData[id];
}

Implicitly define Dictionary pairs [duplicate]

This question already has an answer here:
Flex dictionary literal
(1 answer)
Closed 8 years ago.
Background
Defining an Array is typically demonstrated as var array:Array = new Array(), however, this relegates array assignment to methods like array.push(value) or linear declarations like ...
array[0] = "apple"
array[1] = "orange"
Obviously, a more succinct format is an implicit declaration, where a double bracket is understood to define an array, and the index is automatically handled.
var array:Array = ["apple", "orange"];
The same works for Objects...
var obj:Object = {
"apple":"fritter",
"orange":"pie"
}
The Problem
The problem arises when trying to define a Dictionary's key:value pairs implicitly. Reading the documentation, I was shocked to only find one method on the class. The fact that it extends Object at least means for ... in are available, but that's about where the conveniences end.
Especially since we'll want to use weak keys, the one argument available to Dictionaries would need to be set to true, thereby precluding any kind of implicit definition. The same documentation outlines typical usage in the former (lengthier) format I demonstrated with arrays:
var dict:Dictionary = new Dictionary(true);
dict[key] = "Letters";
That's just not going to fly for complicated structures.
[ redacted with argument ]
Because the docs for both Array & Object never actually explain implicit declarations, I can't help but imagine there might be a way to do so with Dictionaries. Anyone know?
I'm thinking now my only option is to come up with some kind of method which maps a complex object tree to a dictionary... which is dumb, since it'd be faster to just use the long method first demonstrated.
First of all, even if you write the code with Object and don't quote up strings as in original question, you'll not receive the expected results. And if you are attempting to use an object as a key in with statement, the compiler will be confused whether you want dict[obj] or just obj to be assigned a value. So, if you are adding a property to an Object or Dictionary, use the brackets syntax:
obj["apple"]="fritter";
dict[obj]="bar";
Etc.

What exactly is a hash in regards to JSON?

I am learning JSON, but I found out that you can put what are called "hashes" into JSON as well? Where can I find out what a hash is? Or could you explain to me what a hash is? Also, what's a hashmap? I have experience in C++ and C#, and I am learning JS, Jquery, and JSON.
A Hash is a sparse array that uses arbitrary strings/objects (depending on the implementation, this varies across programming languages) rather than plain integers as keys.
In Javascript, any Object is technically a hash (also referred to as a Dictionary, Associative-Array, etc).
Examples:
var myObj = {}; // Same as = new Object();
myObj['foo'] = 'bar';
var myArr = []; // Same as = new Array();
myArr[0] = 'foo';
myArr[1] = 'bar';
myArr['blah'] = 'baz'; // This will work, but is not recommended.
Now, since JSON is basically using JS constructs and some strict guidelines to define portable data, the equivalent to myObj above would be:
{ "foo" : "bar" };
Hope this helps.
Hash = dictionary.
A hash:
{ "key1": "value1", "key2": "value2" }
JSON supports dictionary type elements. People may refer to these as hash tables, which are a type of data structure.
Referring to JSON dictionaries as hash tables would be technically incorrect, however, as there is no particular data structure implementation associated with the JSON data itself.
A hash is a random looking number which is generated from a piece of data and always the same for the same input. For example if you download files from some websites they will provide a hash of the data so you can verify your download is not corrupted (which would change the hash).
Another application of hashes is in a hash table (or hash map). This is a very fast associative data structure where the hashes are used to index into an array. std::unorderd_map in C++ is an example of this.
You could store a hash in JSON as a string for example something like "AB34F553" and use this to verify data.
On json.org, a JSON "object" is "a collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array."
Using the ambiguous term "hash" for a JSON object is confusing. "Hash" is indeed used in the wild as short-hand for: hash map, dictionary, key-value structure, etc. But it is also short-hand for: the hash value that is computed by a hash function.

Repeated Keyword Value in JSON

On JSON.org the essential data structures that JSON represents are given as
A collection of name/value pairs, and
An ordered list of values.
I have not been able to find anywhere whether a second member having the same name as one already parsed into the current object should (a) throw an exception or (b) replace the existing member.
Is this specified anywhere?
What do existing parsers do with repeated names?
EDIT: I am looking to define correct behavior for my parser.
JSON is simply a subset of the object literal notation of JavaScript and as such, is constrained by the same rules - the latest value for repeated keys will override any previously assigned value for that key, within the specific object. Think in terms of assigning a value to an object property; A later assignment will override an earlier one.
To demonstrate this, I have set up an example here. The code is displayed on the page, and as can be seen, the messagebox has the name 'Barney' in it.
Code here -
$(function() {
$('#myButton').click(function(e)
{
var myJsonString = "Person = {'firstName':'Fred','lastName':'Flintstone','firstName':'Barney'}";
eval("(" + myJsonString + ")");
alert(Person.firstName);
});
});
By the Way, I have used eval() here for ease of use. I would recommend using a JSON parser instead of eval() due to security issues.
They last name found by the parser is replaced by the new one. It doesn't throw an expection.
It is simply a Javascript syntax thing.
var json = {};
// lets augment the object
json.one = 1;
json.one = 2; // it gets replaced
I am pretty sure that both behaviors you list would be accepted, along with others (use the first one, use any of them). :-)
That is, such behavior is undefined from JSON specification POV.
As a practical matter, implementations I have used do either one of suggestions you mentioned, or "use the first one".
So no, I would not count on specific behavior given that tools can choose what to do.
Because JSON is simply a subset of Javascript, it largely depends upon the Javascript specification. I don't know personally what the answer is, but I would highly suggest not relying upon the behavior if at all possible.