简体   繁体   中英

What is the C# equivalent of a Javascript array of key/value pairs

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#?

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);

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
[["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 ac# 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.

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

In Javascript it is an Array of Arrays: 在此处输入图片说明

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; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM