【问题标题】:How do I convert an array of objects to a specified object format?如何将对象数组转换为指定的对象格式?
【发布时间】:2020-10-02 18:14:29
【问题描述】:

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

[
{ 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]
}

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

【问题讨论】:

  • 能否请您展示filesList 对象的完整示例?
  • @Paul 我已经更新了对象
  • 试试我的解决方案 - 只是表达,而不是我的对象。我认为它已经开始工作了。

标签: javascript arrays sorting object immutable.js


【解决方案1】:

这是一个快速的方法。我假设您想要一个对象,其中键是 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 对象。
  • 我编辑了我的原始答案。希望这次能帮到你。
【解决方案2】:

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

【讨论】:

  • 已创建具有正确键的对象,但它包含的数组中的所有元素都未定义。见图片:imgur.com/a/Aqkclpe
  • Paul,OP 正在使用 immutable-js(我敢肯定。他所指的类将属性与属性相匹配)。 immutable-js.github.io/immutable-js/docs/#/List 如果你想使用原生数组,你需要调用 .toArray()
  • @user120242是的,就是这样。谢谢!
  • @AndrewChan 你确定要切换到原生数组吗?如果您正在为团队项目或公司工作,破坏他们使用 ImmutableJS 来强制和规范不可变类型可能会导致问题。
  • @user120242 谢谢!我以前不知道 immutableJS :)
猜你喜欢
  • 2021-12-28
  • 1970-01-01
  • 2020-03-16
  • 1970-01-01
  • 2022-01-23
  • 2018-02-13
  • 1970-01-01
相关资源
最近更新 更多