繁体   English   中英

如何将对象数组转换为指定的 object 格式?

[英]How do I convert an array of objects to a specified object format?

这是我可以在数组中访问的一些示例数据。

[
{ filesList: List {size: 2, _origin: 0, _capacity: 2, _level: 5, _root: null, …},
id: "089b5559-171f-46b0-9aa4-1c9fc5013b0b",
name: "Arabidopsis TAIR10",
value: "Arabidopsis",
},
{ filesList: List {size: 2, _origin: 0, _capacity: 2, _level: 5, _root: null, …},
id: "da8f7da3-04e3-4aba-98e3-615e86982a60",
name: "Arabidopsis TAIR10",
value: "Arabidopsis",
},
{ filesList: List {size: 2, _origin: 0, _capacity: 2, _level: 5, _root: null, …},
id: "6b166a90-f73b-4000-b18c-8f7b80513e6d",
name: "Bean GCF_000499845.1",
value: "Bean_reference_genome",
}
]

这是filesList的示例。 它是一个数组。

List {size: 2, _origin: 0, _capacity: 2, _level: 5, _root: null, …}
size: 2
__altered: false
__hash: undefined
__ownerID: undefined
_capacity: 2
_level: 5
_origin: 0
_root: null
_tail: VNode
array: Array(2)
0: File
id: "92cb45c3-91ec-4e1e-8ead-99762465f9e2"
isLocal: true
lastModified: 1591716051955
lastModifiedDate: Tue Jun 09 2020 10:20:51 GMT-0500 (Central Daylight Time) {}
name: "Gene_List_edgr_chickpea.txt"
size: 162890
type: "text/plain"
webkitRelativePath: ""
__proto__: File
1: File
id: "edccae34-71dd-46f7-a4e1-d821e1315f1a"
isLocal: true
lastModified: 1591716038452
lastModifiedDate: Tue Jun 09 2020 10:20:38 GMT-0500 (Central Daylight Time) {}
name: "callset_name_hc.g.vcf.gz"
size: 3891432
type: "application/x-gzip"
webkitRelativePath: ""
__proto__: File
length: 2
__proto__: Array(0)
ownerID: OwnerID {}
__proto__: Object
__proto__: IndexedCollection

我需要将此信息转换为结构如下的 object。

每个键都是来自 Object 的值。 如果键值重复,我想将 append 文件列表数据复制到相同的键。

{ 
Arabidopsis: [file1, file2, file3, file4],
Bean_reference_genome: [file1, file2]
}

我怎么能 go 关于实现这个数据结构?

解决方案取决于filesList的结构。 如果它包含一些简单的 js 数组,它看起来与以下内容非常相似。

 const data = [{ filesList: { size: 2, _origin: 0, _capacity: 2, _level: 5, _root: null, array: ["abc", "cde"] }, id: "089b5559-171f-46b0-9aa4-1c9fc5013b0b", name: "Arabidopsis TAIR10", value: "Arabidopsis", }, { filesList: { size: 2, _origin: 0, _capacity: 2, _level: 5, _root: null, array: ["ghi", "jkl"] }, id: "da8f7da3-04e3-4aba-98e3-615e86982a60", name: "Arabidopsis TAIR10", value: "Arabidopsis", }, { filesList: { size: 2, _origin: 0, _capacity: 2, _level: 5, _root: null, array: ["qwe", "ert"] }, id: "6b166a90-f73b-4000-b18c-8f7b80513e6d", name: "Bean GCF_000499845.1", value: "Bean_reference_genome", } ] const result = data.reduce((acc, entry) => { acc[entry.value] = (acc[entry.value] || []).concat(entry.filesList.array); return acc; }, {}); console.log(result);

这是一个快速的方法。 我假设您想要一个 object ,其中键是name ,值是fileList

var betterStructure = {};
for(var i = 0; i < array.length; i++){
    let {name, filesList} = array[i];
    if(betterStructure.hasOwnProperty(name)){
        betterStructure[name] = betterStructure[name].concat(filesList);
    }
    else{
        betterStructure[name] = filesList;
    }
};

编辑:制作处理重复的逻辑。

暂无
暂无

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

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