【问题标题】:Trying to use ES6 to filter and flatten an array of nested objects尝试使用 ES6 过滤和展平嵌套对象数组
【发布时间】:2018-01-16 15:40:42
【问题描述】:

我是 ES6 的新手,我有一组看起来像这样的对象:

locations: [
  {
    "is_moving": true,
    "uuid": "82fa9dda-e57b-4b3f-99a0-a1db98ae4a19",
    "timestamp": "2017-08-05T04:48:25.526Z",
    "odometer": 0,
    "sample": true,
    "coords": {
      "latitude": 32.7323862,
      "longitude": -117.1939315,
      "accuracy": 1000,
      "speed": -1,
      "heading": -1,
      "altitude": -1
    },
    "activity": {
      "type": "in_vehicle",
      "confidence": 54
    },
    "battery": {
      "is_charging": false,
      "level": 0.5
    },
    "extras": {}
  },
  {
    "event": "motionchange",
    "is_moving": true,
    "uuid": "57a0146a-28b9-4baf-86de-e037843c2d32",
    "timestamp": "2017-08-05T04:48:25.526Z",
    "odometer": 0,
    "coords": {
      "latitude": 32.7323862,
      "longitude": -117.1939315,
      "accuracy": 1000,
      "speed": -1,
      "heading": -1,
      "altitude": -1
    },
    "activity": {
      "type": "in_vehicle",
      "confidence": 54
    },
    "battery": {
      "is_charging": false,
      "level": 0.5
    },
    "extras": {}
  }
]

我想结束的是:

locations: [
  {
    "timestamp": "2017-08-05T04:48:25.526Z",
    "odometer": 0,
    "latitude": 32.7323862,
    "longitude": -117.1939315,
    "accuracy": 1000,
    "speed": -1,
    "heading": -1,
    "altitude": -1
  },
  {
    "timestamp": "2017-08-05T04:48:25.526Z",
    "odometer": 0,
    "latitude": 32.7323862,
    "longitude": -117.1939315,
    "accuracy": 1000,
    "speed": -1,
    "heading": -1,
    "altitude": -1
  }
]

所以我知道我可以过滤掉我不想要的键/值对(感谢https://stackoverflow.com/a/39333664/994275):

locations.map(({ timestamp, odometer, coords }) => ({ timestamp, odometer, coords }))

而且我知道我可以通过以下方式展平对象(感谢https://stackoverflow.com/a/33037683/994275):

Object.assign(
  {}, 
  ...function _flatten(location) { 
    return [].concat(...Object.keys(location)
      .map(k => 
        typeof location[k] === 'object' ?
          _flatten(location[k]) : 
          ({[k]: location[k]})
      )
    );
  }(location)
)

但我正试图将两者结合起来,却惨遭失败。我在地图中添加了扁平化,但这只是返回一个未定义的数组。

我确信有一个简单的解决方法,但目前我还没有解决。任何帮助将不胜感激!

以下是有效的方法(感谢似乎删除了他们的 cmets 的用户):

let newLocations = locations.map(({ is_moving, uuid, timestamp, odometer, coords }) => ({ is_moving, uuid, timestamp, odometer, coords }));
let test = newLocations.map((location) => {
  return Object.assign(
    {}, 
    ...function _flatten(location) { 
      return [].concat(...Object.keys(location)
        .map(k => 
          typeof location[k] === 'object' ?
            _flatten(location[k]) : 
            ({[k]: location[k]})
        )
      );
    }(location)
  )
});

有什么方法可以压缩过滤和展平吗?

【问题讨论】:

    标签: ecmascript-6


    【解决方案1】:

    function body of an arrow function 有两种语法:一种是“块体”,一种是“简洁体”。当您使用“块体”时,您必须使用花括号 { } 将块中包含的语句括起来,如果您不想返回 @,则需要显式的 return 语句987654324@.

    您可以将两个操作组合成一个 map 并使用“简洁正文”语法:

    let newLocations = locations.map(({ is_moving, uuid, timestamp, odometer, coords }) =>
      Object.assign(
        {}, 
        ...function _flatten(location) { 
          return [].concat(...Object.keys(location)
            .map(k => 
              typeof location[k] === 'object' ?
                _flatten(location[k]) : 
                ({[k]: location[k]})
            )
          );
        }({ is_moving, uuid, timestamp, odometer, coords })
      )
    );
    

    【讨论】:

    • 谢谢!不仅是为了答案,也是为了向这个 ES6 菜鸟解释它!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-03
    • 2021-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-15
    • 2020-07-12
    相关资源
    最近更新 更多