首页 > 解决方案 > TypeScript:仅返回 ObjectSet 的第一个值

问题描述

我对 TypeScript 非常陌生,尤其是对 Object(Sets) 的自定义 Palantir 实现。我要存档的内容:我想将 ObjectSet 过滤为某些特定值。然后我想返回这些值中的第一个。事实上,我只想返回一行。到目前为止我所做的:

    @Function()
    public nextUnprocessedValueString(inputObject: ObjectSet<CombinedSentencesForTagging>): ObjectSet<CombinedSentencesForTagging>{
        const result = Objects.search().combinedSentencesForTagging().filter(f => f.customerFeedback.exactMatch('i like it very much.'))
        return result

结果如下所示: 结果

我只需要第一行(或随机行)。

谢谢!

标签: palantir-foundryfoundry-code-repositories

解决方案


试试这个:

@Function()
public nextUnprocessedValueString(inputObject: ObjectSet<CombinedSentencesForTagging>): CombinedSentencesForTagging {
    const result = 
           inputObject.filter(f => f.customerFeedback.exactMatch('i like it very much.'))
                      .orderBy(f => f.customerFeedback.asc())
                      .take(1);
    
    return result[0];
}

以下是我对原始函数所做的更改:

  1. 将返回类型更改为单个CombinedSentencesForTagging对象。
  2. 修改了要在函数参数filter中指定的行上运行inputObject
  3. 使用orderByand takefilter 子句只选择一个过滤结果。

推荐阅读