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



我有一个不确定大小的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 ...
}
]

如何将其转换为键/值 Javascript 对象,其中每个文件都给出一个与其绑定的人员数组,而此数据和仅过滤 type="person" 并得分> 0.8 并在数组内对人员进行排序,从最高分数开始(当实体有超过 1 人时(。

例如,上面的示例应输出:

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
}

我尝试以各种方式减少,映射和过滤,但总是失败。

下面的代码将通过将输入数组减少到对象、过滤掉不需要的实体、根据分数进行反向排序以及将其余实体映射到其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;
}, {});

完整片段:

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);

对于每个对象,执行以下操作。

  • 获取要用作密钥的文件名
  • 根据您的要求过滤Entities(类型="人",得分>0.8(
  • 然后按分数对筛选的实体进行排序
  • 然后将排序的实体映射到其名称并生成名称数组
  • 将其分配为文件名键下的值

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();

最新更新