KeyValuePair in JS

Is there any way to use KeyValuePair in JS and if so what is the proper syntax?

Thanks in advance for any help.

Mitch

I’ll take a guess here since I don’t use JavaScript:

var pair : System.Collections.Generic.KeyValuePair.<string, int> = new System.Collections.Generic.KeyValuePair.<string, int>("key", 9000);

Works like a charm. Thanks FizixMan!

One last question - the KeyValuePair works fine - but how do I make that into a List? I have a C# example - what would be the JS equivalent?

I can’t seem to figure out the syntax. Thanks for any help.

IList<KeyValuePair<float, Agent>> agentNeighbors_ = new List<KeyValuePair<float, Agent>>();

Do you really want to make an IList of those? Would an IDictionary be better managed (forces unique keys)?

Either way, List:
… I… wait a second. I’m not sure at all. Maybe… this?

var agentNeightbours : IList.<KeyValuePair.<float, Agent>> = new List.<KeyValuePair.<float, Agent>>();

Dictionary:

 var agentNeightbours : IDictionary.<float, Agent> = new Dictionary.<float, Agent>();

EDIT: you may need to include the “System.Collections.Generic” namespace into your JS file as well.

Was just doing some Dictionary stuff this morning and I can confirm that this is required (at least when using C#).

Can’t seem to get it right.

var list : List.<System.Collections.Generic.KeyValuePair.<int, int>> = new
List.<System.Collections.Generic.KeyValuePair.<int, int>>();

No worky :frowning:

Anyone with any thoughts? Is there a way to do this in JavaScript?

Thanks for any help.

Mitch

Bump.

Anyone have any insight as to how to get a KeyValuePair to work in JS? The following code throws errors in the log:

var list : List.<System.Collections.Generic.KeyValuePair.<int, int>>;

Thanks for any help.

Mitch

Again, do you need to have a List of KeyValuePairs? What is it that you’re trying to accomplish?

Yes - a List of KeyValuePairs. The syntax we came up with yesterday throws errors on the log.

I’m trying to convert a C# program to JS. The List of KeyValuePairs is the only thing that has me stumped.

Maybe this is just a limitation of the syntax the Unity devs added to JavaScript.

Do your KeyValuePairs have multiple types or are they always <int,int>? If so you could always roll your own:

public class IntIntKeyValuePair
{
	var Key : int;
	var Value : int;
	public IntIntKeyValuePair(var key : int, var value : int)
	{
		this.Key = key;
		this.Value = value;
	}
}

var set : List.<IntIntKeyValuePair> = new List.<IntIntKeyValuePair>();
set.Add(new IntIntKeyValuPair(1, 9000));

Debug.Log("KEY: " + set[0].Key);
Debug.Log("Value: " + set[0].Value);

By the way, do your KeyValuePairs have unique keys? Or are there duplicates?

EDIT: just realized, KeyValuePair is a struct, so rolling your own class like this will have different behavioural implications to consider than a KeyValuePair.

I was using <int,int> just for simplicity at this point. You may be right - this could be a limitation of UnityScript. If anyone knows different, please chime in.

I was thinking of rolling my own class - your example looks great - thanks Fizix - I’ll go ahead and give it a whirl!

Just be careful, there are some differences between that and KeyValuePair, for example:

var kvp : KeyValuePair.<int, int> = new KeyValuePair.<int, int>(1, 1);
var kvp2 : KeyValuePair.<int, int> = new KeyValuePair.<int, int>(1, 1);
Debug.Log("ARE EQUAL: " + (kvp == kvp2)); //true


var kvp : IntIntKeyValuePair = new IntIntKeyValuePair(1, 1);
var kvp2 : IntIntKeyValuePair = new IntIntKeyValuePair(1, 1);
Debug.Log("ARE EQUAL: " + (kvp == kvp2)); //false


var kvp : KeyValuePair.<int, int> = new KeyValuePair.<int, int>(1, 1);
var kvp2 : KeyValuePair.<int, int> = kvp;
kvp2.Key = 9000;
Debug.Log("KEYS: " + kvp.Key + " -> " + kvp2.Key);//KEYS: 1 -> 9000

var kvp : IntIntKeyValuePair = new IntIntKeyValuePair(1, 1);
var kvp2 : IntIntKeyValuePair = kvp
kvp2.Key = 9000;
Debug.Log("KEYS: " + kvp.Key + " -> " + kvp2.Key);//KEYS: 9000 -> 9000

Thanks for the heads up. I’ll continue to see if KeyValuePair is valid in JS while I roll my own class for now.

There is also Hashtable. That is what I have been using.

var hash : HashTable = HashTable();

Well if MitchStan’s KeyValuePairs have unique keys, then he should be using Dictionary.

hashtable is not type fixed, which also means you get no type error checking at compile time … thats the main reason you use generics, that you have a defined state at any time as incorrect data will fire errors already on compile :slight_smile:

hashtable is good for string → string / numeric storage but basically only there (or if you have no plan what generics are naturally or are to lazy to learn it or need a generic type independent data contrainer for network serialization)