将表示歌曲细节的字符串数组合并到具有艺术家,专辑和歌曲的JS对象中

时间:2015-05-30 02:34:32

标签: javascript arrays json node.js lodash

我尝试根据文件名创建一个JSON对象,该对象是我的音乐库的目录。

文件名的格式如下:

"<artist> - <album> - <track no/title>.<ext>"
// example: "Aeon-Lost - Constant Power - 01 Nebulous.ogg"

我有一个将此字符串转换为对象的函数:

var generate_music_lib = function (s) {
    var song_props = s.split(' - ');
    var song_name = song_props[2];
    return {
        artist: path.basename(song_props[0]),
        albums: [
            {
                title: song_props[1],
                songs: [
                    path.basename(song_name, path.extname(song_name))
                ]
            }
        ]
    };
};

我将所有音频文件名的字符串存储在一个数组中:

var songs = glob.sync('**/*.ogg', { cwd: MUSIC_DIR });

然后使用generate_music_lib function遍历我的歌曲数组,并将它们存储在名为music_lib的新数组中:

_.each(_.map(songs, generate_music_lib), function (song) {
    music_lib.push(song);
});

所以字符串数组:

["Aeon-Lost - Constant Power - 01 Nebulous.ogg"]

存储为对象数组:

[
  {
    "artist": "Aeon-Lost",
    "albums": [
      {
        "title": "Constant Power",
        "songs": [
          "01 Nebulous"
        ]
      }
    ]
  }
]

这是我的主要问题。我如何获取这样的字符串数组:

[ "Bongripper - Satan Worshipping Doom - 01 Hail.ogg",
  "Bongripper - Satan Worshipping Doom - 02 Satan.ogg",
  "Bongripper - Satan Worshipping Doom - 03 Worship.ogg",
  "Bongripper - Satan Worshipping Doom - 04 Doom.ogg",
  "Bongripper - Miserable - 01 Endless.ogg",
  "Bongripper - Miserable - 02 Descent.ogg",
  "Bongripper - Miserable - 03 Into Ruin.ogg" 
  "Earthless - Live at Guadalest -SC19- - 01 From the Ages.ogg",
  "Earthless - Live at Guadalest -SC19- - 02 Godspeed.ogg" ]

将其转化为:

[
  {
    "artist": "Bongripper",
    "albums": [
      {
        "title": "Satan Worshipping Doom",
        "songs": [
          "01 Hail",
          "02 Satan",
          "03 Worship",
          "04 Doom"
        ]
      },
      {
        "title": "Miserable",
        "songs": [
          "01 Endless",
          "02 Descent",
          "03 Into Ruin"
        ]
      }
    ]
  },
  {
    "artist": "Earthless",
    "albums": [
      {
        "title": "Live at Guadalest -SC19-",
        "songs": [
          "01 From The Ages",
          "02 Godspeed"
        ]
      }
    ]
  }
]

所以,如果&#34;艺术家&#34;的价值键匹配,通过附加&#34;专辑&#34;来合并对象。和#34;歌曲&#34;阵列。否则,它是数组中的另一个对象。任何帮助表示赞赏。谢谢!

1 个答案:

答案 0 :(得分:0)

伪代码:

_.each(listOfSongs, function(element, index){
  var musicLibrary = {}; 
  /** the resulting object with all songs, that you will return at the end of this function
  **/
  var details = element.split(" - ") 
/** this will break if you have more than 2 of these character sequences in your titles, so maybe error-checking here, if the resulting element's length is 3, (if not, throw 'invalid song title')
**/

  if (details[0]){//artist exists
    if (details[1]){//album exists
      if (details[2]){//song exists
      }
    }
  }
return musicLibrary;
});

拆分字符串应该可以方便地访问其余部分 我会让你弄明白如何:
1.当一个人不存在时添加一个新的艺术家,如果有的话,就更深入了 2.当一个相册不存在时添加新相册,如果有相册则更深入 3.如果没有艺术家+专辑的匹配则添加新歌曲,如果匹配则不执行任何操作/抛出/覆盖。

只需在else条款中实施这些内容,您就可以了。 考虑使用三元运算符重构可读性。