let myObj= [
{ "ean":1, "name":'prod1', "attibutes":[{"attr": 100,"color": "green"},{"attr": 200,"color": "red"}] },
{ "ean":2, "name":'prod1', "attibutes":[{"attr": 100,"color": "white"},{"attr": 200,"color": "blu"}] }
];
function toCsv(arrOfObj){
// array of objects with nested objects
// keys for arr of obj (rows) is numeric
// key for elements of nested obj is a name
// I transform each row in an array
const csvString = [
...arrOfObj.map(item => [
item.ean,
item.name,
//NO: passes 0:object,1:obj, Object.entries(item.attibutes).map(([k,v]) => `${k}:${v}`)
Object.keys(item.attibutes).map(row => [ Object.keys(item.attibutes[row]).map( elkey => elkey + ':' + item.attibutes[row][elkey] )])
])]
.map(e => e.join(";"))
.join("
");
console.log(csvString);
return csvString;
}
//TEST
toCsv(myObj);