Skip to content

Instantly share code, notes, and snippets.

@jurgob
Created December 18, 2015 15:55
Show Gist options
  • Save jurgob/0b40b1de75c20de61938 to your computer and use it in GitHub Desktop.
Save jurgob/0b40b1de75c20de61938 to your computer and use it in GitHub Desktop.
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