4

What does this represent in javascript:

[["January", 10], ["February", 8], ["March", 4], ["April", 13], ["May", 17], ["June", 9]]

And secondly, what is the C# equivalent?

Array of arrays?? But each inner array is essentially a key/value pair, so that's not right.

How would one generate this JS object in C#?

3
  • 2
    Equivalent in what context? Do you want to use it to look up values, like you could do if it were passed to the Map constructor in JavaScript? Do you want a list of tuples? (.NET has tuples.) Commented Jan 13, 2018 at 21:52
  • ok, the answer is "a List of Lists of string". However, all of the answers below make more sense than that answer for various reasons (type safety, no uniqueness constraint, etc.). This "thing" is used in the "Flot" Javacript plotting library which needs this format to work correctly. So, an ajax call to a c# web api controller and convert the data to a List of Lists of strings. Weird...anyway, I guess just up-vote each of the answer and pick the best - the best I can. Thank you everyone for taking the time. Commented Jan 13, 2018 at 22:37
  • A list of lists of string is not correct to produce this JavaScript value – you’ll get ["January", "10"] instead of ["January", 10]. Are you producing JSON? Did you try tuples with your JSON serializer (which one)? Commented Jan 13, 2018 at 22:49

5 Answers 5

9

If it’s a map, then the equivalent for efficient lookups in C# would be a Dictionary<TKey, TValue>:

var months = new Dictionary<string, int>()
{
    {"January", 1}, {"February", 2}, {"March", 3},
    {"April", 4}, {"May", 5}, {"June", 6}
};

Now, if it’s an array of tuples, C# 7 has native tuple support:

var months = new[] 
{
    ("January", 1), ("February", 2), ("March", 3),
    ("April", 4), ("May", 5), ("June", 6)
};

And you can even name the members of the tuple like so:

var months = new (string Name, int Number)[]
{
    ("January", 1), ("February", 2), ("March", 3),
    ("April", 4), ("May", 5), ("June", 6)
};

Console.WriteLine(months[0].Name);
Sign up to request clarification or add additional context in comments.

Comments

3
[["January", 10], ["February", 8], ["March", 4], ["April", 13], ["May", 17], ["June", 9]]

What does this represent in javascript:

Well [] denotes an array. So the outer portion is an array. Since each array element is also delimited by [] each element is also an array.

And secondly, what is the C# equivalent?

Well pragmatically you can't assign that value to anything, it would cause a compile time error. If you used something like Json.Net to convert it to a c# object, the absolute closest thing (outside -> in) would be a jagged array.

object[][]

Normally jagged arrays are not really that useful. In this case, my opinion would be to covert the array into a List<DateTime> because Lists's are much easier to work with, and it appears that each array element contains a month and a day, which is easier to work with as DateTime object.

Comments

3

Its Dictionary

Syntax: Dictionary<TKey, TValue>

var myDictionary = new Dictionary<int, string>();
myDictionary.Add(10, "January");
myDictionary.Add(8, "February");
myDictionary.Add(4, "March");
myDictionary.Add(13, "April");
myDictionary.Add(17, "May");

You can also have a List of KeyValuePairs

var list = new List<KeyValuePair<string, int>>()
{
    new KeyValuePair<string, int>("January", 10),
    new KeyValuePair<string, int>("February", 8),
    new KeyValuePair<string, int>("March", 4),
    new KeyValuePair<string, int>("April", 13),
    new KeyValuePair<string, int>("May", 17),
};

So what is the difference between List<KeyValuePair<T1, T2>> and Dictionary<T1, T2>?

The answer is the List does not enforce uniqueness of the Key.

Meaning you can do this with a List and KeyValuePair.

//Record with same key
new KeyValuePair<string, int>("May", 17),
new KeyValuePair<string, int>("May", 17),

But you cannot do it with a Dictionary

    myDictionary.Add(17, "May");
    myDictionary.Add(17, "May"); //ERROR

1 Comment

This is basically correct but I think using the KeyValuePair<TKey, TValue> type for the list elements is not a good idea.
2

What does this represent in javascript:

It can be map:

let m = new Map([["February", 8], ["March", 4], ["April", 13]])

Or just array of arrays:

let a = [["February", 8], ["March", 4], ["April", 13]];

And secondly, what is the C# equivalent?

It can be Dictionary<string, int> or array of arrays

2 Comments

Actually, since c# is stongly typed it can't be a jagged array unless you convert the numbers to strings as well...
@ZoharPeled: object[]
1

In Javascript it is an Array of Arrays: enter image description here

Since C# is a strongly typed language it is unconventional, and not recommended, to represent this data in its current form but you can, like ErikPhillips says in the comments below, like so:

object[] array1 = { "February", 8 };

Since the "outer" Array is an Array of two types, string and int, it would be more conventional strongly typed C# best practices to create a "model" of each month day array element in something like the C# code below, since C# arrays are more frequently an array of one type, for example an array of string or an array of int or in the code below, conventional strongly typed C# an array of MonthDayModel:

public class TestClass {

    public TestClass(){
        MonthDayModel[] arr = new MonthDayModel[10];
        arr[0] = new MonthDayModel() {
            Month = "February",
            Day = 8
        };
        arr[1] = new MonthDayModel() {
            Month = "March",
            Day = 4
        };
        //...
    }
}

public class MonthDayModel {
    public string Month { get; set; }
    public int Day { get; set; }
}

4 Comments

Or a dictionary of string and int or an array of tuples of sting and int or... (There are more options...)
object[] array1 = { "February", 8 }; works just fine. Your statement You cannot represent represent this data in C# is simply wrong.
@ErikPhilips ok you got me :)
I would say you could do this, but it's generally not good practice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.