如何将包含对象的对象转换为数组数组?

时间:2018-02-12 18:44:16

标签: javascript arrays

如何转换此数据结构:

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 = [ 
            ['campaign_lead_id', 'date', 'campaign_name', 'influencer_name', 'influencer_email'],
            ['2', '2017-11-11T22:19:33.538000+00:00', 'IOT course fall 2017', null, 'test_user_1@2key.co'],
            ['1', '2017-11-09T20:43:26.953000+00:00', 'IOT course fall 2017', null, 'test_user_1@2key.co']
        ];

7 个答案:

答案 0 :(得分:1)

以下是我认为您可能正在寻找的代码 将对象传递给makeArray函数,它将返回所需的数组。

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

注意:仅对对象对象有效。

答案 1 :(得分:0)

使用此代码段: Object.keys()函数返回一个包含对象键名称的数组。 Object.values()返回对象的值数组。

var arraysOfarray = [
    Object.keys(data[0]),
    Object.values(data[0])
];

console.log(arraysOfarray);

答案 2 :(得分:0)

正如您所说:使用forEach

  • 使用Object.keys方法获取密钥。
  • 对于每次迭代,使用Object.values函数。

&#13;
&#13;
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);
&#13;
.as-console-wrapper {
  max-height: 100% !important
}
&#13;
&#13;
&#13;

资源

答案 3 :(得分:0)

我发现papa解析对这类事情非常有帮助。如果您可以重新构建数据,使其成为数组中的对象列表(注意我如何使用[而不是{创建 const数据),那么我认为可以通过一行代码完成:

&#13;
&#13;
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);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/4.3.7/papaparse.min.js"></script>
&#13;
&#13;
&#13;

答案 4 :(得分:0)

只需使用下面的缩小

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 5 :(得分: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));

答案 6 :(得分:0)

试试这个例子。即使属性的顺序不同,它也会起作用。

&#13;
&#13;
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);
&#13;
&#13;
&#13;