How can I transform this data structure:
const data = {
0: {
campaign_lead_id: 2,
date: "2017-11-11T22:19:33.538000+00:00",
campaign_name: "IOT course fall 2017",
influencer_name: null,
influencer_email: "test_user_1@key"
},
1: {
campaign_lead_id: 1,
date: "2017-11-09T20:43:26.953000+00:00",
campaign_name: "IOT course fall 2017",
influencer_name: null,
influencer_email: "test_user_1@key"
}
}
to array of arrays, where first array contains key names, and other arrays contain key values, like this:
const CSVdata = [
['campaign_lead_id', 'date', 'campaign_name', 'influencer_name', 'influencer_email'],
['2', '2017-11-11T22:19:33.538000+00:00', 'IOT course fall 2017', null, '[email protected]'],
['1', '2017-11-09T20:43:26.953000+00:00', 'IOT course fall 2017', null, '[email protected]']
];
-
1Can you show your first approach to solve it?Ele– Ele2018年02月12日 18:46:40 +00:00Commented Feb 12, 2018 at 18:46
-
I've tried to do it with forEach and map methods, but unsuccessfullyRoman Shevchenko– Roman Shevchenko2018年02月12日 18:49:39 +00:00Commented Feb 12, 2018 at 18:49
-
Can you share that? you're going to get a lot of help! :-)Ele– Ele2018年02月12日 18:51:18 +00:00Commented Feb 12, 2018 at 18:51
-
you might find Papa.parse helpfuluser4490801– user44908012018年02月12日 19:03:01 +00:00Commented Feb 12, 2018 at 19:03
-
Nothing to share, I just tried to use those methods, but I don't think that they can help me in this particular caseRoman Shevchenko– Roman Shevchenko2018年02月12日 19:07:17 +00:00Commented Feb 12, 2018 at 19:07
7 Answers 7
Below is the code I think you might looking for.
Pass object to the makeArray function it will return the required array.
const data = {
0: {
campaign_lead_id: 2,
date: "2017-11-11T22:19:33.538000+00:00",
campaign_name: "IOT course fall 2017",
influencer_name: null,
influencer_email: "test_user_1@key"
},
1: {
campaign_lead_id: 1,
date: "2017-11-09T20:43:26.953000+00:00",
campaign_name: "IOT course fall 2017",
influencer_name: null,
influencer_email: "test_user_1@key"
}
};
function makeArray(data){
var initialKeys = Object.keys(data);
var answer = [Object.keys(data[initialKeys[0]])];
for(let i=0;i<initialKeys.length;i++) {
answer.push(Object.values(data[i]));
}
return answer;
}
const CSVdata = makeArray(data);
console.log(CSVdata);
Note : Only valid for object of objects.
Comments
User this code snippet: Object.keys() function return an array containing names of the keys of the object. Object.values() return an array of values of the object.
var arraysOfarray = [
Object.keys(data[0]),
Object.values(data[0])
];
console.log(arraysOfarray);
2 Comments
As you said: Using forEach
- Get the keys using
Object.keysmethod. - For each iteration use
Object.valuesfunction.
const data = {
'0': {
campaign_lead_id: 2,
date: "2017-11-11T22:19:33.538000+00:00",
campaign_name: "IOT course fall 2017",
influencer_name: null,
influencer_email: "test_user_1@key"
},
'1': {
campaign_lead_id: 1,
date: "2017-11-09T20:43:26.953000+00:00",
campaign_name: "IOT course fall 2017",
influencer_name: null,
influencer_email: "test_user_1@key"
}
};
var array = [['campaign_lead_id', 'date', 'campaign_name', 'influencer_name', 'influencer_email']];
Object.keys(data).forEach((k) => array.push(Object.values(data[k])));
console.log(array);
.as-console-wrapper {
max-height: 100% !important
}
Resources
Comments
Just use reduce like below
const data = {
0: {
campaign_lead_id: 2,
date: "2017-11-11T22:19:33.538000+00:00",
campaign_name: "IOT course fall 2017",
influencer_name: null,
influencer_email: "test_user_1@key"
},
1: {
campaign_lead_id: 1,
date: "2017-11-09T20:43:26.953000+00:00",
campaign_name: "IOT course fall 2017",
influencer_name: null,
influencer_email: "test_user_1@key"
}
}
let arr = Object.keys(data).sort().reduce((a, b) => {
a.push(Object.values(data[b]));
return a;
}, [Object.keys(data[0])]);
console.log(arr);
Comments
const data = {
0: {
campaign_lead_id: 2,
date: "2017-11-11T22:19:33.538000+00:00",
campaign_name: "IOT course fall 2017",
influencer_name: null,
influencer_email: "test_user_1@key"
},
1: {
campaign_lead_id: 1,
date: "2017-11-09T20:43:26.953000+00:00",
campaign_name: "IOT course fall 2017",
influencer_name: null,
influencer_email: "test_user_1@key"
}
}
const csvData = (data) => {
const docs = Object.values(data);
return [Object.keys(docs[0])].concat(docs.map(d => Object.values(d)))
};
console.log(csvData(data));
Comments
I find papa parse quite helpful for this sort of thing. If you can restructure your data so that it's a list of objects within an array (notice how i use [ rather than { to create the const data ), then I think it can be done with a single line of code:
const data = [
{
campaign_lead_id: 2,
date: "2017-11-11T22:19:33.538000+00:00",
campaign_name: "IOT course fall 2017",
influencer_name: null,
influencer_email: "test_user_1@key"
},
{
campaign_lead_id: 1,
date: "2017-11-09T20:43:26.953000+00:00",
campaign_name: "IOT course fall 2017",
influencer_name: null,
influencer_email: "test_user_1@key"
}
]
var new_data = Papa.parse(Papa.unparse(data,{header:true}))["data"];
console.dir(new_data);
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/4.3.7/papaparse.min.js"></script>
Comments
Try this example. It will work even if properties are in different order.
const data = {
0: {
campaign_lead_id: 2,
date: "2017-11-11T22:19:33.538000+00:00",
campaign_name: "IOT course fall 2017",
influencer_name: null,
influencer_email: "test_user_1@key"
},
1: {
date: "2017-11-09T20:43:26.953000+00:00",
campaign_name: "IOT course fall 2017",
campaign_lead_id: 1,
influencer_name: null,
influencer_email: "test_user_1@key"
}
};
let csv = Object.values(data).reduce((acc, cur) => {
acc.push(acc[0].map(title => cur[title]));
return acc;
}, [Object.keys(data[0])]);
console.log(csv);