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

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

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

每个键都是对象的值。如果键值重复,我想将 filesList 数据附加到同一个键。

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

我怎样才能实现这个数据结构?

这里有一个快速的方法。我假设您想要一个对象,其中键是 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;
    }
};

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

解决方案取决于 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);