[英]Javascript - transform array of javascript objects into a sorted key/value javascript object
我有一個不確定大小的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對象,其中每個文件給出與其相關聯的Persons數組,而此數據僅根據type =“ person”進行過濾,並且得分> 0.8,並通過以最高分 (實體的PERSON大於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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.