2017-04-18 84 views
2

我很抱歉,如果它是一個愚蠢的問題,我正在爲一個JS考試學習,我採取了一個隨機的分配任務,我在JSFiddle中按年排序來自URL的JSON數據。 代碼放在爲:JSFiddle對象排序算法崩潰

https://jsfiddle.net/gs6eey97/ (i apologize for all the comments). 

第一個問題是,控制檯將返回排序列表,但每種分選的元素在排序列表中出現兩次(Console result)。我想過加入一個if語句剛剛整理之前的數據被放在

minYear.push(minYearPom); 
minSort.push(propPom); 

以下內容::

if (propPom != minSort[minSort.length - 1]) { 
     minYear.push(minYearPom); 
     minSort.push(propPom); 
     } 

和鉻停止respo陣列通過改變以下過濾重複nding,我已經試過刪除

minYear.push(minYearPom); 

,因爲我意識到我並不真的需要它,而Chrome停止響應,我添加了簡單的console.log看到某些變量的值來揣摩出了什麼問題是,Chrome停止響應。朋友似乎無法找到代碼的問題,所以如果任何人都可以找到問題,我會非常感激。

回答

2

當你得到對象的對象,

{ 
    movies: { 
     tt0111161: { 
      Title: "The Shawshank Redemption", 
      Year: "1994", 
      Runtime: "142", 
      Director: "Frank Darabont", 
      Actors: [ 
       "Tim Robbins", 
       "Morgan Freeman", 
       "Bob Gunton", 
       "William Sadler" 
      ], 
      Language: [ 
       "English" 
      ], 
      imdbRating: "9.3", 
      imdbVotes: "138" 
     }, 
     // ... 
    } 
} 

你需要採取的鑰匙,就像tt0111161等,並應用鍵陣列上的排序:

var data = { movies: { tt0111161: { Title: "The Shawshank Redemption", Year: "1994", Runtime: "142", Director: "Frank Darabont", Actors: ["Tim Robbins", "Morgan Freeman", "Bob Gunton", "William Sadler"], Language: ["English"], imdbRating: "9.3", imdbVotes: "138" }, tt0068646: { Title: "The Godfather", Year: "1972", Runtime: "175", Director: "Francis Ford Coppola", Actors: ["Marlon Brando", "Al Pacino", "James Caan", "Richard S. Castellano"], Language: ["English", "Italian", "Latin"], imdbRating: "9.2", imdbVotes: "96" } } }, 
 
    keys = Object.keys(data.movies); 
 

 
keys.sort(function (a, b) { 
 
    return data.movies[a].Year - data.movies[b].Year; 
 
}); 
 

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

1

我們ü本身Object.prototype.values()獲取電影作爲對象的數組:

Object.values()方法返回給定的對象自身 枚舉屬性值的數組,以相同的順序作爲由 for...in loop提供(不同之處在於一個for-in循環枚舉了原型鏈中的 屬性)。

let movies = Object.values(data.movies); 

/* 
movies = [ 
    { "Title": "The Shawshank Redemption", ... }, 
    { "Title": "The Godfather", ... }, 
    ... 
]; 
*/ 

currentYear用於計算膜的年齡

我們然後用Array.prototype.sort()電影排序:

sort()方法到位並返回排序的數組元素 數組。排序不一定穩定。默認排序順序 根據字符串Unicode代碼點。

movies.sort((a, b) => a.Year - b.Year); 

對於我們使用Array.prototype.reduce()

reduce()方法的膜的平均年齡應用於對一個儲液器的功能和陣列中的每個 元件(從左至右),以減少它以單一的 值。

let avg = movies.reduce((result, currentMovie) => result + (currentYear - currentMovie.Year), 0)/movies.length; 

在片斷也可用於但用於排序/平均部分不相關:

Arrow function (... => ...)

箭頭函數表達式具有比功能 表達較短的語法和不不綁定自己的this,arguments,super,或者new.target。這些函數表達式最適合非方法 函數,並且它們不能用作構造函數。

Template literals (`...${...}...`)

模板文字是字符串常量,允許嵌入的表達式。 您可以使用多行字符串和字符串插值功能與 他們。在之前版本的ES2015規範中,它們被稱爲「模板字符串」。

let data = { "movies":{ "tt0111161":{ "Title":"The Shawshank Redemption", "Year":"1994", "Runtime":"142", "Director":"Frank Darabont", "Actors":["Tim Robbins", "Morgan Freeman", "Bob Gunton", "William Sadler"], "Language":["English"], "imdbRating":"9.3", "imdbVotes":"138" }, "tt0068646":{ "Title":"The Godfather", "Year":"1972", "Runtime":"175", "Director":"Francis Ford Coppola", "Actors":["Marlon Brando", "Al Pacino", "James Caan", "Richard S. Castellano"], "Language":["English", "Italian", "Latin"], "imdbRating":"9.2", "imdbVotes":"96" }, "tt0071562":{ "Title":"The Godfather: Part II", "Year":"1974", "Runtime":"200", "Director":"Francis Ford Coppola", "Actors":["Al Pacino", "Robert Duvall", "Diane Keaton", "Robert De Niro"], "Language":["English", "Italian", "Spanish", "Latin", "Sicilian"], "imdbRating":"9.1", "imdbVotes":"64" }, "tt0468569":{ "Title":"The Dark Knight", "Year":"2008", "Runtime":"152", "Director":"Christopher Nolan", "Actors":["Christian Bale", "Heath Ledger", "Aaron Eckhart", "Michael Caine"], "Language":["English", "Mandarin"], "imdbRating":"9.0", "imdbVotes":"137" }, "tt0110912":{ "Title":"Pulp Fiction", "Year":"1994", "Runtime":"154", "Director":"Quentin Tarantino", "Actors":["Tim Roth", "Amanda Plummer", "Laura Lovelace", "John Travolta"], "Language":["English", "Spanish", "French"], "imdbRating":"8.9", "imdbVotes":"108" } } } 
 
    movies = Object.values(data.movies), 
 
    currentYear = new Date().getFullYear(); 
 

 
movies.sort((a, b) => a.Year - b.Year); 
 
let avg = movies.reduce((result, currentMovie) => { 
 
       return result + (currentYear - currentMovie.Year); 
 
      }, 0)/movies.length; 
 

 
movies.forEach(movie => console.log(`Title: ${movie.Title} (${movie.Year})`)); 
 
console.log(`Average age: ${avg}`);