C# 数组中的键值对功能

问题描述

我正在尝试为我的主要数据转换工作创建一个“多维”锯齿状数组。我希望最里面的数组具有对象的键值对行为,但我不知道使用什么语法:

function mapOrder (array,order,key) {

  array.sort( function (a,b) {
    var A = a[key],B = b[key];

    if (order.indexOf(A) > order.indexOf(B)) {
      return 1;
    } else {
      return -1;
    }

  });

  return array;
};


reordered_array_a = mapOrder(courseData[0][0],orderTemplateSimple[0],id);

这种语法中的大部分内容对我来说都是新的,所以请告诉我我犯了哪些其他错误

如果数组中的键值对是不可能的,我将不得不使用未命名的索引引用,对吗?那将在构建时使用 () 并使用 [0] 作为参考,是吗?这个数组还能在对象外保存混合数据类型吗?

ps:将处理此数据的函数示例:

{{1}}

其中 orderTemplateSample[index] 是一个数字数组,用于转换从 courseData 中“提取”数组的顺序。

我想在那里有 id 键引用,但如果我必须用理论上可行的数字替换它?

解决方法

让我们从inmost类型开始,也就是

  {id = 1,question = "question",answer = "answer"},

不能成为键值,因为它具有三个属性:id,question,answer。 但是,您可以将其转换为命名元组

  (int id,string question,string answer)

声明将是

  (int id,string answer)[][][] courseData = 
     new (int,string,string)[][][]
  {
      new (int,string)[][]//chapter 1
      {
        new (int,string)[]
        {
           // Long form
          (id : 1,question : "question",answer : "answer"),// Short form: we can skip id,answer names 
          (2,"question","answer"),}
      }
   };

现在你有一个 array(确切地说是数组的数组):

   int course = 1;
   int chapter = 1;
   int question = 2;

   // - 1 since arrays are zero based
   string mySecondAnswer = courseData[course - 1][chapter - 1][question - 1].answer;