This is some example data that I have access to in an array.
[
{ 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",
}
]
Here is an example of the filesList
object. It is an array.
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
I need to convert this information to an object that is structured below.
With each key being the value from the Object. If the key value is duplicate I want to append the filesList data to the same key.
{
Arabidopsis: [file1, file2, file3, file4],
Bean_reference_genome: [file1, file2]
}
How could I go about achieving this data structure?
The solution depends on the structure of filesList
. If it contains some simple js array, it would look quite similiar to the following.
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);
Here is a quick way of doing it. I'm assuming that you want an object where the key is the name
and the value is the 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;
}
};
Edit: Made the logic that handles duplicates.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.