【问题标题】:How can i get the entity value after checking the type?检查类型后如何获取实体值?
【发布时间】:2018-05-28 08:24:27
【问题描述】:
    (4) [{…}, {…}, {…}, {…}]
0: {entity: "lovely meeting", type: "Calendar.Subject", startIndex: 53, endIndex: 66, score: 0.9183444}
1: {entity: "california", type: "Calendar.Location", startIndex: 27, endIndex: 36, score: 0.922538459}
2: {entity: "today", type: "builtin.datetimeV2.date", startIndex: 18, endIndex: 22, resolution: {…}}
3: {entity: "4am", type: "builtin.datetimeV2.time", startIndex: 41, endIndex: 43, resolution: {…}}length: 4__proto__: Array(0)

这 4 个元素是 Entities 的一部分,我想要检查类型是否等于假设 Calender.Location 并且值得注意的是,我们并不总是拥有可以拥有一个或没有的 4 个元素,并且即使我们确实有 4 个实体类型 Calender.Location 不一定是索引号 1,我尝试过的包括 testvar.entities.lengthtestvar.entities[0].type 不知道每个的 ID 是不可能的。

我想要的示例:

我想在实体中解析Json 类型的Calendar.location 输入,它可能存在也可能不存在,并且它的确切ID 未知(范围在0 到4 之间),如果我找到它,我应该返回在这种情况下与它关联的实体california

【问题讨论】:

    标签: json node.js parsing


    【解决方案1】:

    const test =[
      {entity: "lovely meeting", type: "Calendar.Subject", startIndex: 53, endIndex: 66, score: 0.9183444},
      {entity: "california", type: "Calendar.Location", startIndex: 27, endIndex: 36, score: 0.922538459},
      {entity: "today", type: "builtin.datetimeV2.date", startIndex: 18, endIndex: 22, resolution: {}},
      {entity: "4am", type: "builtin.datetimeV2.time", startIndex: 41, endIndex: 43, resolution: {}}
    ]
    
    
    const getentitiesByType  = (arr,type) => {
      
     for(let i in arr ){
        if(arr[i].type === type){
           return arr[i].entity;
        }
      }
    
    };
    
    console.log(getentitiesByType(test,"Calendar.Location"))

    【讨论】:

      猜你喜欢
      • 2017-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多