0

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]']
 ];
asked Feb 12, 2018 at 18:44
6
  • 1
    Can you show your first approach to solve it? Commented Feb 12, 2018 at 18:46
  • I've tried to do it with forEach and map methods, but unsuccessfully Commented Feb 12, 2018 at 18:49
  • Can you share that? you're going to get a lot of help! :-) Commented Feb 12, 2018 at 18:51
  • you might find Papa.parse helpful Commented 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 case Commented Feb 12, 2018 at 19:07

7 Answers 7

1

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.

answered Feb 12, 2018 at 19:14
Sign up to request clarification or add additional context in comments.

Comments

0

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);
answered Feb 12, 2018 at 18:51

2 Comments

Thank you, but I need to build arrays of values dynamically, I mean instead of Object.values(data[0]) I need somehow to loop through all object of data
Yea, i show you key areas of the solution. can't you do it by your self because once you know main areas, it so much simple.
0

As you said: Using forEach

  • Get the keys using Object.keys method.
  • For each iteration use Object.values function.

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

answered Feb 12, 2018 at 18:56

Comments

0

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);

answered Feb 12, 2018 at 19:18

Comments

0

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));

answered Feb 12, 2018 at 19:19

Comments

0

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>

answered Feb 12, 2018 at 19:13

Comments

0

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);

answered Feb 12, 2018 at 23:10

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.