从对象数组中对项目进行分组和排序

时间:2014-12-29 18:45:40

标签: javascript jquery underscore.js

这是我的对象数组:

var array = [
 {season:  1, episode:  2, notes: "..."},
 {season:  1, episode:  2, notes: "..."},
 {season:  1, episode:  1, notes: "..."},
 {season: -1, episode: -1, notes: "..."},
 {season: -1, episode: -1, notes: "..."},
 {season: -1, episode: -1, notes: "..."},
 {season:  3, episode:  1, notes: "..."},
 {season:  13, episode:  12, notes: "..."},
 {season:  2, episode:  1, notes: "..."},
];

我想按季节对这个数组进行排序,结果必须是:

var sorted_array = [
 {season: -1, episode: -1, notes: "..."},
 {season: -1, episode: -1, notes: "..."},
 {season: -1, episode: -1, notes: "..."},
 {season:  1, episode:  1, notes: "..."},
 {season:  1, episode:  2, notes: "..."},
 {season:  1, episode:  2, notes: "..."},
 {season:  2, episode:  1, notes: "..."},
 {season:  3, episode:  1, notes: "..."},
 {season:  13, episode:  12, notes: "..."},
];

按季节和剧集对此数组进行分组,因此必须使用另一个键,结果必须如下:

var final_array = [

 { "S-1 E-1": [
   {season: -1, episode: -1, notes: "..."},
   {season: -1, episode: -1, notes: "..."},
   {season: -1, episode: -1, notes: "..."},
 ] },
 { "S01 E01": [
   {season:  1, episode:  1, notes: "..."}
 ] },
 { "S01 E02": [
   {season:  1, episode:  2, notes: "..."},
   {season:  1, episode:  2, notes: "..."}
 ] },
 { "S02 E01": [
   {season:  2, episode:  1, notes: "..."}
 ] },
 { "S03 E01": [
   {season:  3, episode:  1, notes: "..."}
 ] },
 { "S13 E12": [
   {season:  13, episode:  12, notes: "..."}
 ] }

];

有关如何从final_array

生成array数组的任何想法

由于

2 个答案:

答案 0 :(得分:0)

您可以使用_.groupBy立即进行分组:

var grouped = _.groupBy(array, function (el) { 
    var s = el.season,
        e = el.episode;
    return "S" + (s > 0 && s < 10 ? "0" : "") + s +
           " E" + (e > 0 && e < 10 ? "0" : "") + e;
});

这将产生一个对象,其分组值为键:

{ 
    "S-1 E-1": [
        {season: -1, episode: -1, notes: "..."},
        {season: -1, episode: -1, notes: "..."},
        {season: -1, episode: -1, notes: "..."},
    ],
    "S01 E01": [
        {season:  1, episode:  1, notes: "..."}
    ],
    "S01 E02": [
        {season:  1, episode:  2, notes: "..."},
        {season:  1, episode:  2, notes: "..."}
    ],
    "S02 E01": [
        {season:  2, episode:  1, notes: "..."}
    ],
    "S03 E01": [
        {season:  3, episode:  1, notes: "..."}
    ],
    "S13 E12": [
        {season:  13, episode:  12, notes: "..."}
    ]
}

如果您想在季节/剧集排序顺序中迭代它,您可以对其键进行排序并迭代这些键:

_.sortBy(Object.keys(grouped), function (el) { return el; }).forEach(function (key) {
    console.log(key + ":");
    console.log(grouped[key]);
});

http://jsfiddle.net/g370ycmn/

答案 1 :(得分:0)

这非常非常丑陋,但只是想知道你是否需要未来的某些部分...... DEMO

输入:

 var sArray = [
      {season:  1, episode:  2, notes: "..."},
      {season:  1, episode:  2, notes: "..."},
      {season:  1, episode:  1, notes: "..."},
      {season: -1, episode: -1, notes: "..."},
      {season: -1, episode: -1, notes: "..."},
      {season: -1, episode: -1, notes: "..."},
      {season:  3, episode:  1, notes: "..."},
      {season:  13, episode:  12, notes: "..."},
      {season:  2, episode:  1, notes: "..."},
];

输出:

'S-1 E-1':[{season:-1, episode:-1, notes:"..."}, {season:-1, episode:-1, notes:"..."}, {season:-1, episode:-1, notes:"..."}], 
'S01 E01':[{season:1, episode:1, notes:"..."}], 
'S01 E02':[{season:1, episode:2, notes:"..."}, {season:1, episode:2, notes:"..."}], 
'S02 E01':[{season:2, episode:1, notes:"..."}], 
'S03 E01':[{season:3, episode:1, notes:"..."}], 
'S13 E12':[{season:13, episode:12, notes:"..."}]

代码(来自其中一条评论):

//Sort array by season and episode keys
//0 means same, 1 means greater and -1 means less
sArray.sort(function (a, b) {
    if (a.season == b.season) {
        return (a.episode < b.episode) ? -1 : (a.episode > b.episode) ? 1 : 0;
    } else {
        return (a.season < b.season) ? -1 : 1;
    }
});

var c = sArray.length,
    sfArray = {};
//Populate object
for (var i = 0; i < c; i++) {
    //Define the key:conact season and episode while checking if the number ∈ [0,9], if true add '0' before
    var key = 'S' + ((sArray[i].season < 10 && sArray[i].season > -1) ? '0' + sArray[i].season : sArray[i].season) +
        ' E' + ((sArray[i].episode < 10 && sArray[i].episode > -1) ? '0' + sArray[i].episode : sArray[i].episode);

    //Check if key exists, otherwise create it as Array
    if (!(key in sfArray)) 
        sfArray[key] = [];

    //Add element to key
    sfArray[key].push(sArray[i]);
}