简体   繁体   English

分割对象数组

[英]Split Array of Objects

Is there a way of changing my array of objects into a few seperate arrays for each property of my objects? 有没有一种方法可以将对象的数组更改为对象的每个属性的几个单独的数组?

For example, I have this: 例如,我有这个:

[ 
    { 
        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 
    }
]

And I want it to look like this: 我希望它看起来像这样:

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

So that I can use it with plot.ly. 这样我就可以在plot.ly中使用它。

You can use the map function: 您可以使用地图功能:

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

You can use reduce and destructuring 您可以使用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} 

You could iterating the array and push the same keys to the same key in an object. 您可以迭代数组并将相同的键推入对象中的相同键。

This answer utilized an object, opposite of the wanted results in single variables. 这个答案利用了一个对象,它与单个变量中想要的结果相反。 The advantage is to keep all data combined and not spreaded over the code in different variable. 这样做的好处是可以使所有数据组合在一起,而不是散布在不同变量的代码中。

The use is easy with just the reference by property. 仅按属性引用即可使用。

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

IMHO it will be difficult to make without loop. 恕我直言,将很难没有循环。

This code might be simpler: 这段代码可能更简单:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM