[Solved] move key to sub array js [closed]


You can use .map(), .concat() and Object.assign() methods to get the resultant array:

let data = [
  {Name: 'Color', Data:[{Id: 1, Name: 'Red'}, {Id: 2, Name: 'Yellow'}, {Id: 3, Name: 'Blue'}, {Id: 4, Name: 'Green'}, {Id: 7, Name: 'Black'}]},
  {Name: 'Size', Data:[{Id: 8, Name: 'S'}, {Id: 11, Name: 'M'}, {Id: 12, Name: 'L'}, {Id: 13, Name: 'XL'}, {Id: 14, Name: 'XXL'}]}
];

let result = [].concat(
  ...data.map(({Name, Data}) => Data.map(o => Object.assign({}, o, {optionName: Name})))
);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

solved move key to sub array js [closed]