Created
December 18, 2015 15:55
-
-
Save jurgob/0b40b1de75c20de61938 to your computer and use it in GitHub Desktop.
This file contains 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
function unwind(arr, expandProp,renameProp){ | |
'use strict'; | |
return arr | |
.map((el) => { | |
return el[expandProp] | |
.map((subProp) => { | |
var subEl = Object.assign({},el,{[renameProp]:subProp}); | |
delete subEl[expandProp]; | |
return subEl; | |
}); | |
}) | |
.reduce( (a,b) => {return a.concat(b); } ) | |
}; | |
var arr = [{tag:'a',list:[1,2,3]},{tag:'b',list:[1,2]}]; | |
var expandProp = 'list'; | |
var renameProp = 'value'; | |
unwind(arr, expandProp, renameProp); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment