Javascript 如何在typescript中比较json数组

Javascript 如何在typescript中比较json数组,javascript,json,typescript,Javascript,Json,Typescript,我在比较typescript函数中json文件中的数组时遇到了这个问题。 index.ts是这样的:提示1:不要硬编码对上下文变量的引用,如“Profession”提示2:条件可以嵌套,n级深度。将条件转换为后缀符号。这些是我在解决这个问题时得到的一些提示 import conditionInput from "./condition.json"; /** * Evaluate a condition against the context. * @param cond

我在比较typescript函数中json文件中的数组时遇到了这个问题。 index.ts是这样的:提示1:不要硬编码对上下文变量的引用,如“Profession”提示2:条件可以嵌套,n级深度。将条件转换为后缀符号。这些是我在解决这个问题时得到的一些提示

import conditionInput from "./condition.json";

/**
 * Evaluate a condition against the context.
 * @param condition A domain-specific language (DSL) JSON object.
 * @param context An object of keys and values
 * @return boolean
 */
function evaluate(
  condition: typeof conditionInput,
  context: { [key: string]: string | undefined }
  // count=0
): boolean {
  // Your task is to implement this function such that it evaluates the imported condition.json against the context. Strings prefixed with '$' are variables and should have their values drawn from the context.
  // Hint 1:  Do not hard-code references to context variables like 'Profession'
  // Hint 2:  Conditions can be nested, n-levels deep
  // Convert the condition into postfix-notation

  // Ideally, one value must remain in the result stack with the last operation's result
  return false;
}

/**
 * Click "run" to execute the test cases, which should pass after your implementation.
 */
(function () {
  const cases = [
    {
      context: {
        State: "Alabama",
        Profession: "Software development",
      },
      expected: true,
    },
    {
      context: {
        State: "Texas",
      },
      expected: true,
    },
    {
      context: {
        State: "Alabama",
        Profession: "Gaming",
      },
      expected: false,
    },
    {
      context: {
        State: "Utah",
      },
      expected: false,
    },
    {
      context: {
        Profession: "Town crier",
      },
      expected: false,
    },
    {
      context: {
        Profession: "Tradesperson",
      },
      expected: true,
    },
  ];

  for (const c of cases) {
    const actual = evaluate(conditionInput, c.context);
    console.log(actual === c.expected ? "yay :-)" : "nay :-(");
  }
})();
consistion.json是这样的:

[
  "OR",
  [
    "AND",
    ["==", "$State", "Alabama"],
    ["==", "$Profession", "Software development"]
  ],
  ["AND", ["==", "$State", "Texas"]],
  ["OR", ["==", "$Profession", "Tradesperson"]]
]
函数是条件运算符(toCheck){
开关(toCheck){
案例“和”:
案例“或”:
返回true;
违约:
返回false;
}
}
函数等参器(toCheck){
开关(toCheck){
案例“==”:
案例“”:
案例“=”:
案例“!=”:
返回true;
违约:
返回false;
}
}
函数计算(a,运算符,b){
返回操作员(a、b);
}
函数symbol运算器(symbol){
开关(符号){
案例“==”:返回doubleEqual;
案例“”:返回值大于;
大小写“>=”:返回lessThanEqual;

case'这是作业吗?不,我是typescript新手,一个朋友把它作为一个挑战给了我,但我真的不能专注于这个问题,因为我不知道typescript是如何工作的。请问一些更具体的问题,关于typescript的哪些部分让你感到困惑。Stack overflow不是一个网站,你只是发布一个问题,人们会帮你解决建议您首先尝试关注问题,并找出如何使用伪代码或您熟悉的语言解决问题。然后,您尽最大努力用typescript/javascript实现它。如果遇到问题,请将其发布在此处。这实际上是我认识的一家公司的编码测试!参考:。向此人发出的请求谁否决了我的答案。请你留下一条评论,说明原因。请不要只是粘贴代码而不做任何解释。这个网站是用于教学和学习的。仅仅发布一个解决方案,你只是解决了当前的问题,但没有帮助提问者理解如何在未来解决类似问题。它是在抛出错误我不喜欢typescript。为什么每次声明和使用变量时都要定义一个类型??