对JavaScript的递归

对JavaScript的递归,javascript,c#,typescript,recursion,Javascript,C#,Typescript,Recursion,如何将递归C#代码转换为JavaScript bool IsVisibleForExposure => TotalExposure.HasValue || Children.Any(i => i.IsVisibleForExposure); 我试过使用Lodash,但似乎不起作用: getIsVisibleForExposure(item: ILimitItem) { let result = item.totalExposure !== null || _.some(item.

如何将递归C#代码转换为JavaScript

bool IsVisibleForExposure => TotalExposure.HasValue || Children.Any(i => i.IsVisibleForExposure);
我试过使用Lodash,但似乎不起作用:

getIsVisibleForExposure(item: ILimitItem) {
 let result = item.totalExposure !== null || _.some(item.children, i => this.getIsVisibleForExposure(i));
 return result;
}
数据结构如下所示:

JavaScript C# 我基本上需要知道ILimitItem对象或其任何子对象(具有children属性)的totalExposure是否不为NULL

下面是示例数据,但它当然是更复杂的层次结构或树结构

const data: ILimitItem = [
  {
    totalExposure: null,
    chilren: [
       {
          totalExposure: null
          children: [
              totalExposure: 100
              children: []
          ]
       }
    ]
  }
  ...
]
假设:

  • 如果根目录或任何嵌套的
    TotalExposure
    有任何赋值,则需要返回
    true
  • 如果未设置任何
    TotalExposure
    ,则返回
    false
  • 和TotalExposure可设置为
    null
    或任何
    falsy
  • 然后,此代码将执行您期望的操作:

    接口项{
    总曝光量:个数;
    儿童:项目[]
    }
    函数getIsVisibleForExposure(项目:ILimitItem){
    return!!item.totalExposure | |
    一些(i=>getIsVisibleForExposure(i));
    }
    常量数据:ILIMITEM={
    totalExposure:null,
    儿童:[{
    totalExposure:null,
    儿童:[]
    }]
    }
    让结果=getIsVisibleForExposure(数据);
    
    控制台日志(结果)
    TotalExposure不可为Null我的朋友请提供一些模拟数据。请修复您的帖子-您的C#code没有按照发布的方式编译(根据前面的评论,
    int TotalExposure
    没有声明为nullable),JS中也没有
    接口
    关键字(这是TypeScript片段吗?@CoolBots-是的,它显然是TypeScript。打字稿很好用。但是没有明确的期望,就没有办法提供答案。两者之间的区别是什么!!item.totalExposure和item.totalExposure!==null?两个求反运算符(
    !!
    )一起将强制任何变量为布尔值-相比之下,
    ==null
    是专门测试某些值不等于
    null
    且仅
    null
    而不考虑其他假值可能性。以下是使用“双不”逻辑运算符的参考:!!
    class LimitItem {
       int? TotalExposure { get; set; }
       LimitItem[] Children { get; set; }
       bool IsVisibleForExposure => TotalExposure.HasValue || Children.Any(i => i.IsVisibleForExposure);
    }
    
    const data: ILimitItem = [
      {
        totalExposure: null,
        chilren: [
           {
              totalExposure: null
              children: [
                  totalExposure: 100
                  children: []
              ]
           }
        ]
      }
      ...
    ]