Created
June 5, 2017 17:45
-
-
Save sxwebdev/a8c15d03e027e307cca08f01164c6b87 to your computer and use it in GitHub Desktop.
array to object es6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const arrayToObject = (array) => { | |
return array.reduce(function(result, item) { | |
var key = Object.keys(item)[0]; | |
if(Array.isArray(item[key])) { | |
item[key] = arrayToObject(item[key]); | |
} | |
result[key] = item[key]; | |
return result; | |
}, {}); | |
} | |
let new_arr = []; | |
data.map((val,key) => { | |
let obj = arrayToObject(val.historyRecord) | |
new_arr.push(obj); | |
}); | |
console.log(new_arr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment