2017-08-08 125 views
1

我是全新的,以ES6的数组,我有,看起来像对象的数组:尝试使用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) 
) 

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

我确定有一个简单的解决方法,但它在这一点上无法解决。任何帮助将不胜感激!

这里是工作(感谢谁似乎已经消除了他们的意见的用户):

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) 
) 
}); 

有没有办法凝聚过滤和扁平化?

回答

0

function body of an arrow function有两种语法:一种带有“块体”,另一种带有“简明主体”。在使用“块体”时,如果不想返回undefined,则必须使用大括号{ }在块中包含所包含的语句,并且需要明确的return语句。

您可以将两个操作结合成一个单一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 }) 
) 
); 
+0

谢谢!不仅仅是为了答案,而是为了向ES6 noob解释它! – JulieMarie