拆分对象数组

时间:2016-11-17 14:08:53

标签: javascript

有没有办法将我的对象数组更改为我的对象的每个属性的几个单独的数组?

例如,我有这个:

[ 
    { 
        date: Mon Aug 08 2016 16:59:16 GMT+0200 (CEST),
        visits: 0,
        hits: 578,
        views: 5131 
    },
    { 
        date: Tue Aug 09 2016 16:59:16 GMT+0200 (CEST),
        visits: -1,
        hits: 548,
        views: 722 
    },
    { 
        date: Wed Aug 10 2016 16:59:16 GMT+0200 (CEST),
        visits: -1,
        hits: 571,
        views: 4772 
    }
]

我希望它看起来像这样:

var dates = ["date1", "date2", ...],
    visits = ["visit1", "visit2", ...],
    hits = ["hits1", "hits2", ...],
    views = ["view1", "view2", ...];

这样我就可以将它与plot.ly.

一起使用

4 个答案:

答案 0 :(得分:7)

您可以使用map功能:



var array =  [ { "date": 'Mon Aug 08 2016 16:59:16 GMT+0200 (CEST)',
    visits: 0,
    hits: 578,
    views: 5131 },
  { "date": 'Tue Aug 09 2016 16:59:16 GMT+0200 (CEST)',
    visits: -1,
    hits: 548,
    views: 722 },
  { "date": 'Wed Aug 10 2016 16:59:16 GMT+0200 (CEST)',
    visits: -1,
    hits: 571,
    views: 4772 }];

var dates = array.map(item => item.date);
var visits = array.map(item => item.visits);
var hits = array.map(item => item.hits);
var views = array.map(item => item.views);

console.log(dates, visits, hits, views);




答案 1 :(得分:1)

您可以迭代数组并将相同的键推送到对象中的相同键。

这个答案使用了一个对象,与单个变量中的所需结果相反。优点是保持所有数据的组合,而不是在不同变量的代码上传播。

仅使用属性引用即可轻松使用。

var data = [{ "date": 'Mon Aug 08 2016 16:59:16 GMT+0200 (CEST)', visits: 0, hits: 578, views: 5131 }, { "date": 'Tue Aug 09 2016 16:59:16 GMT+0200 (CEST)', visits: -1, hits: 548, views: 722 }, { "date": 'Wed Aug 10 2016 16:59:16 GMT+0200 (CEST)', visits: -1, hits: 571, views: 4772 }],
    object = { date: [], visits: [], hits: [], views: [] },
    keys = Object.keys(object);

data.forEach(function (a) {
    keys.forEach(function (k) {
       object[k].push(a[k]);
    });
});

console.log(object);

答案 2 :(得分:1)

您可以使用reduce和解构



var arr = [
  { date: "Mon Aug 08 2016 16:59:16 GMT+0200 (CEST)", visits: 0, hits: 578, views: 5131 },
  { date: "Tue Aug 09 2016 16:59:16 GMT+0200 (CEST)", visits: -1, hits: 548, views: 722 },
  { date: "Wed Aug 10 2016 16:59:16 GMT+0200 (CEST)", visits: -1, hits: 571, views: 4772 }
];

let keys = ['date','visits','hits','views'],
    [dates, visits, hits, views] = arr.reduce( (a,b) => {
	return keys.map( (x,i) => {a[i].push(b[x])}), a;
}, [[],[],[],[]]);

console.log(dates, visits, hits, views)

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




答案 3 :(得分:0)

恕我直言,没有循环就很难做到。

此代码可能更简单:

var data =  [
{ date: "Mon Aug 08 2016 16:59:16 GMT+0200 (CEST)", visits: 0, hits: 578, views: 5131 },
{ date: "Tue Aug 09 2016 16:59:16 GMT+0200 (CEST)", visits: -1, hits: 548, views: 722 },
{ date: "Wed Aug 10 2016 16:59:16 GMT+0200 (CEST)", visits: -1, hits: 571, views: 4772 }
];

var getArrayByValue = function(data, key) {
    var result = [];
    for (i in data) {
        result.push(data[i][key])
    }
    return result;
}

var dates = getArrayByValue(data, 'date')
var visits = getArrayByValue(data, 'visits')
var hits = getArrayByValue(data, 'hits')
var views = getArrayByValue(data, 'views')