过滤嵌套的数组和对象

时间:2018-04-27 16:58:40

标签: arrays typescript object filter

我有一个包含2个复杂对象的数组:

(2) [{…}, {…}]

0:
  name: 'item1'
  data:
    fileExtension: undefined
    force_distribution: Array(9)
      0: {Fx: Array(76), x: Array(76), Fxcum: Array(76)}
      1: {y: Array(22), Fx: Array(22), Fxcum: Array(22)}
      2: {Fx: Array(92), z: Array(92), Fxcum: Array(92)}
      3: {x: Array(22), Fycum: Array(22), Fy: Array(22)}
      4: {y: Array(92), Fy: Array(92), Fycum: Array(92)}
      5: {Fy: Array(76), z: Array(76), Fycum: Array(76)}
      6: {x: Array(92), Fz: Array(92), Fzcum: Array(92)}
      7: {y: Array(76), Fz: Array(76), Fzcum: Array(76)}
      8: {Fz: Array(22), z: Array(22), Fzcum: Array(22)}
      length:9
1: 
  name: 'item2'
  data:
    fileExtension: undefined
    force_distribution: Array(9)
      0: {Fx: Array(76), x: Array(76), Fxcum: Array(76)}
      1: {y: Array(22), Fx: Array(22), Fxcum: Array(22)}
      2: {Fx: Array(182), z: Array(182), Fxcum: Array(182)}
      3: {x: Array(22), Fycum: Array(22), Fy: Array(22)}
      4: {y: Array(182), Fy: Array(182), Fycum: Array(182)}
      5: {Fy: Array(76), z: Array(76), Fycum: Array(76)}
      6: {x: Array(182), Fz: Array(182), Fzcum: Array(182)}
      7: {y: Array(76), Fz: Array(76), Fzcum: Array(76)}
      8: {Fz: Array(22), z: Array(22), Fzcum: Array(22)}
      length:9

如果用户选择x和Fx有没有办法在不使用循环的情况下轻松获得以下内容?我知道如何使用过滤器,但我很难应用于嵌套数组和对象。

0:
  name: 'item1'
  data:
    fileExtension: undefined
    force_distribution: Array(9)
      0: {Fx: Array(76), x: Array(76), Fxcum: Array(76)}
      length:1
1: 
  name: 'item2'
  data:
    fileExtension: undefined
    force_distribution: Array(9)
      0: {Fx: Array(76), x: Array(76), Fxcum: Array(76)}
      length:1

1 个答案:

答案 0 :(得分:0)

我不清楚您的过滤逻辑是什么(您说用户选择xFy,但示例结果包含xFx的项目)但您可以使用mapfilter使用object spread复制其余内容时使用const filtered = original.map(item => ({ ...item, // not needed if top level array items only have 'data' key data: { ...item.data, force_distribution: item.data.force_distribution.filter(/* your filter logic here */) } })); Main port

src: url('../webagency_images/fonts/fontawsome-webfont.eot');