map 操作
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>利用map reduce将对象转为数组并且拉平</title> </head> <body> <script> var keys = ['a','b','c'] //js Map 方法 var dataObj = { a:[1], b:[2], c:[3,5] } console.log(keys.map(key => dataObj[key]))//[[1],[2],[3,5]] var result = keys.map(key => dataObj[key]) .reduce((prev,next) => prev.concat(next),[]) console.log(result) //[1,2,3,5] </script> </body> </html>