简体   繁体   English

Javascript-将JavaScript对象数组转换为排序的键/值javascript对象

[英]Javascript - transform array of javascript objects into a sorted key/value javascript object

I have a array of javascript objects of indefinite size: 我有一个不确定大小的javascript对象数组:

var arr = [      
 {
        "Entities": 
          [
            {
              "BeginOffset": 28,
              "EndOffset": 35,
              "Score": 0.9945663213729858,
              "Text": "Tunisie",
              "Type": "LOCATION"
            },
            {
              "BeginOffset": 60,
              "EndOffset": 71,
              "Score": 0.8412493228912354,
              "Text": "Al HuffPost",
              "Type": "PERSON"
            },
            {
              "BeginOffset": 60,
              "EndOffset": 71,
              "Score": 0.9412493228912354,
              "Text": "trump",
              "Type": "PERSON"
            } 
          ],
        "File": "article1.com"
 },
 {
        "Entities": 
          [
            {
              "BeginOffset": 28,
              "EndOffset": 35,
              "Score": 0.9945663213729858,
              "Text": "france",
              "Type": "LOCATION"
            },
            {
              "BeginOffset": 60,
              "EndOffset": 71,
              "Score": 0.7412493228912354,
              "Text": "john locke",
              "Type": "PERSON"
            },
            {
              "BeginOffset": 60,
              "EndOffset": 71,
              "Score": 0.9412493228912354,
              "Text": "sawyer",
              "Type": "PERSON"
            } 
          ],
        "File": "anotherarticle.com"
      },
      {
        //and so on ...
 }
]

How to transform it to a key/value Javascript object where each file gives an array of the Persons tied to it while this data and filtering only on type="person" and score > 0.8 and sorting inside the array the Persons by starting with the highest scores (when there are more than 1 PERSON for Entities). 如何将其转换为键/值Javascript对象,其中每个文件给出与其相关联的Persons数组,而此数据仅根据type =“ person”进行过滤,并且得分> 0.8,并通过以最高分 (实体的PERSON大于1)。

For example, the example above should output: 例如,上面的示例应输出:

var finalObject =    {
  "article1.com": ["trump", "Al HuffPost"],//tunisisa not here because entity is a LOCATION
  "anotherarticle.com": ["sawyer"] //john locke not here because score <0.8
}

I tried reducing, mapping and filtering in all ways but always fail. 我尝试过以各种方式进行缩小,映射和过滤,但始终失败。

The code below will create the requested output by reducing the input array to an object, filtering out the unwanted entities, reverse sorting based on score, and mapping the remaining entities to their Text attributes: 下面的代码通过将输入数组简化为一个对象,过滤掉不需要的实体,基于得分进行反向排序,并将其余实体映射到它们的Text属性,来创建请求的输出:

const result = arr.reduce((a, {Entities, File}) => {
  a[File] = Entities
              .filter(({Type, Score}) => Type === 'PERSON' && Score > 0.8)
              .sort((a, b) => b.Score - a.Score)
              .map(({Text}) => Text);
  return a;
}, {});

Complete snippet: 完整代码段:

 const arr = [{ "Entities": [{ "BeginOffset": 28, "EndOffset": 35, "Score": 0.9945663213729858, "Text": "Tunisie", "Type": "LOCATION" }, { "BeginOffset": 60, "EndOffset": 71, "Score": 0.8412493228912354, "Text": "Al HuffPost", "Type": "PERSON" }, { "BeginOffset": 60, "EndOffset": 71, "Score": 0.9412493228912354, "Text": "trump", "Type": "PERSON" } ], "File": "article1.com" }, { "Entities": [{ "BeginOffset": 28, "EndOffset": 35, "Score": 0.9945663213729858, "Text": "france", "Type": "LOCATION" }, { "BeginOffset": 60, "EndOffset": 71, "Score": 0.7412493228912354, "Text": "john locke", "Type": "PERSON" }, { "BeginOffset": 60, "EndOffset": 71, "Score": 0.9412493228912354, "Text": "sawyer", "Type": "PERSON" } ], "File": "anotherarticle.com" } ]; const result = arr.reduce((a, {Entities, File}) => { a[File] = Entities .filter(({Type, Score}) => Type === 'PERSON' && Score > 0.8) .sort((a, b) => b.Score - a.Score) .map(({Text}) => Text); return a; }, {}); console.log(result); 

For each Object, do the following. 对于每个对象,请执行以下操作。

  • Get the file name to be used as the key 获取用作密钥的文件名
  • Filter the Entities according to your requirement (type="person" and score > 0.8) 根据您的要求过滤Entities (类型=“人”且得分> 0.8)
  • Then sort the filtered entities by score 然后按分数对过滤后的实体进行排序
  • Then map the sorted entities to their names and generate the names array 然后将排序后的实体映射到它们的名称并生成names数组
  • Assign it as the value under the file name key 将其分配为文件名键下的值

 var arr = [ { "Entities": [ { "BeginOffset": 28, "EndOffset": 35, "Score": 0.9945663213729858, "Text": "Tunisie", "Type": "LOCATION" }, { "BeginOffset": 60, "EndOffset": 71, "Score": 0.8412493228912354, "Text": "Al HuffPost", "Type": "PERSON" }, { "BeginOffset": 60, "EndOffset": 71, "Score": 0.9412493228912354, "Text": "trump", "Type": "PERSON" } ], "File": "article1.com" }, { "Entities": [ { "BeginOffset": 28, "EndOffset": 35, "Score": 0.9945663213729858, "Text": "france", "Type": "LOCATION" }, { "BeginOffset": 60, "EndOffset": 71, "Score": 0.7412493228912354, "Text": "john locke", "Type": "PERSON" }, { "BeginOffset": 60, "EndOffset": 71, "Score": 0.9412493228912354, "Text": "sawyer", "Type": "PERSON" } ], "File": "anotherarticle.com" } ]; function createObject() { var result = {}; arr.forEach(function (item) { var fileName = item['File']; result[fileName] = item["Entities"] .filter(function (entity) { return (entity['Type'] === 'PERSON' && entity['Score'] > 0.8) }) .sort(function (entity1, entity2) { return (entity1['Score'] > entity2['Score']) ? -1 : 1; }) .map(function (entity) { return entity['Text'] }); }); console.log(result); } createObject(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM