代码之家  ›  专栏  ›  技术社区  ›  Anji

返回大多数发行专辑的年份的函数

  •  1
  • Anji  · 技术社区  · 7 年前

    以下是甲壳虫乐队多年来发行的专辑列表。编写一个返回年份的函数 与大多数专辑发行。如果有一年,则返回字符串,否则返回数组。

    var beatles_discography = {
     "Please Please Me": 1963,
     "With the Beatles": 1963,
     "A Hard Day's Night ": 1964, 
     "Beatles for Sale ": 1964, 
     "Twist and Shout ": 1964, 
     "Help ": 1965, 
     "Rubber Soul ": 1965,
     "Revolver": 1966,
     "Sgt. Pepper's Lonely Hearts Club Band": 1967,
     "Magical Mystery Tour ": 1967, 
     "The Beatles ": 1968, 
     "Yellow Submarine ": 1969 ,
     "Abbey Road": 1969, 
     "Let It Be ": 1970
    }
    

    到目前为止我试着喜欢这个:

    var x = {
     "Please Please Me": 1963,
     "With the Beatles": 1963,
     "A Hard Day's Night ": 1964, 
     "Beatles for Sale ": 1964, 
     "Twist and Shout ": 1964, 
     "Help ": 1965, 
     "Rubber Soul ": 1965,
     "Revolver": 1966,
     "Sgt. Pepper's Lonely Hearts Club Band": 1967,
     "Magical Mystery Tour ": 1967, 
     "The Beatles ": 1968, 
     "Yellow Submarine ": 1969 ,
     "Abbey Road": 1969, 
     "Let It Be ": 1970
    }
    var y = {};
    
    for (var key in x){
      y[x[key]] = y[x[key]] ? y[x[key]] + 1: 1;
    }
    var arr = Object.keys(y);
    function getYear(arr){
      for (var m=0; m<arr.length -1; m++){
      if(y[arr[0]] > y[arr[1]]){
        return arr[0];
      }else{
        var temp = [];
        if(y[m] == y[m+1]){
          temp.push(arr[m],arr[m+1]);
        }
        return temp;
      }
      }
    }
    
    console.log(getYear(arr));
    

    我对这段代码的预期输出是1964年,因为在列表中我今年只重复了3次。如果对象中有1965也有3次,那么我需要返回一个类似于[19641965]的数组。谢谢你的帮助。

    3 回复  |  直到 7 年前
        1
  •  2
  •   Dinesh K    7 年前
    var albums = {
        "Please Please Me": 1963,
        "With the Beatles": 1963,
        "A Hard Day's Night ": 1964,
        "Beatles for Sale ": 1964,
        "Twist and Shout ": 1964,
        "Help ": 1963,
        "Rubber Soul ": 1965,
        "Revolver": 1966,
        "Sgt. Pepper's Lonely Hearts Club Band": 1967,
        "Magical Mystery Tour ": 1967,
        "The Beatles ": 1968,
        "Yellow Submarine ": 1969,
        "Abbey Road": 1969,
        "Let It Be ": 1970
    }
    
    
    function getYear(albums) {
        var albumOccurrence = {};
        var max = 0;
        var res = [];
        for (var key in albums) {
            albumOccurrence[albums[key]] = albumOccurrence[albums[key]] ? albumOccurrence[albums[key]] + 1 : 1;
            if (albumOccurrence[albums[key]] > max)
                max = albumOccurrence[albums[key]];
        }
        console.log(max, albumOccurence);
        for (var occurrence in albumOccurrence) {
            if (albumOccurrence[occurrence] == max) {
                res.push(occurrence);
            }
        }
        if (res.length == 1) {
            res = res[0];
        }
        return res;
    }
    console.log(getYear(albums));
    

    这就是你要找的吗?

        2
  •  1
  •   quirimmo    7 年前

    您可以从开始的数据结构创建一个新的数据结构,将年份作为键,然后将相册数组作为值。

    然后你可以循环你的新数据结构,得到专辑的最大数量和与Max对应的密钥(年):

    var x = {
     "Please Please Me": 1963,
     "With the Beatles": 1963,
     "A Hard Day's Night ": 1964, 
     "Beatles for Sale ": 1964, 
     "Twist and Shout ": 1964, 
     "Help ": 1965, 
     "Rubber Soul ": 1965,
     "Revolver": 1966,
     "Sgt. Pepper's Lonely Hearts Club Band": 1967,
     "Magical Mystery Tour ": 1967, 
     "The Beatles ": 1968, 
     "Yellow Submarine ": 1969 ,
     "Abbey Road": 1969, 
     "Let It Be ": 1970
    };
    const res = {};
    for (let key in x) {
      if (!res[x[key]]){
        res[x[key]] = [];
      }
      res[x[key]].push(key);
    }
    
    let max = 0;
    let maxKeys = [];
    for (let key in res) {
      if (max < res[key].length) {
        max = res[key].length;
        maxKeys = [key];
      }
      else if (max === res[key].length) {
        maxKeys.push(key);
      }
    }
    maxKeys.forEach(key => {
      console.log('Max year:', key, '\n', 'Albums number', res[key].length, '\n', 'Albums', res[key]);
    });
        3
  •  0
  •   brk    7 年前

    可以创建对象数组。每个对象可以有一年,一组电影和电影的数量。然后在对数组进行排序时,将给出具有最高影片的对象

    let data = {
      "Please Please Me": 1963,
      "With the Beatles": 1963,
      "A Hard Day's Night": 1964,
      "Beatles for Sale": 1964,
      "Twist and Shout": 1964,
      "Help": 1965,
      "Rubber Soul": 1965,
      "Revolver": 1966,
      "Sgt. Pepper's Lonely Hearts Club Band": 1967,
      "Magical Mystery Tour": 1967,
      "The Beatles": 1968,
      "Yellow Submarine": 1969,
      'Abbey Road': 1969,
      "Let It Be": 1970
    }
    
    let yearObj = []
    // iterating the object and creating an array of objects
    for (let keys in data) {
      // check in the array , if there exist an object which have smae year
      let checkIfKeyPresent = yearObj.findIndex(item => {
        return item.year === data[keys]
      })
      // if the year exist then just update movies array and the count
      if (checkIfKeyPresent !== -1) {
        yearObj[checkIfKeyPresent]['movieNo'] += 1;
        yearObj[checkIfKeyPresent]['movies'].push(keys)
      } else {
        // if it does not exist then create a new object with following keys and
        // push it to the main array
        yearObj.push({
          year: data[keys],
          movieNo: 1,
          movies: [keys]
        })
      }
    }
    let k = yearObj.sort((a, b) => {
      return b.movies.length - a.movies.length
    })
    console.log(yearObj[0])
    推荐文章