2017-02-27 28 views
0

如何过滤高于90的分数?

const shows = [ 
 
    { 
 
    title: `legion`, 
 
    season: 1, 
 
    score: 94, 
 
    }, { 
 
    title: `sneaky pete`, 
 
    season: 1, 
 
    score: 100, 
 
    }, { 
 
    title: `santa clarita diet`, 
 
    season: 1, 
 
    score: 71, 
 
    }, { 
 
    title: `riverdale`, 
 
    season: 1, 
 
    score: 87, 
 
    }, { 
 
    title: `the young pope`, 
 
    season: 1, 
 
    score: 74, 
 
    }, { 
 
    title: `a series of unfortunate events`, 
 
    season: 1, 
 
    score: 94, 
 
    }, { 
 
    title: `taboo`, 
 
    season: 1, 
 
    score: 78, 
 
    }, { 
 
    title: `colony`, 
 
    season: 2, 
 
    score: 100, 
 
    }, { 
 
    title: `24: legacy`, 
 
    season: 1, 
 
    score: 57, 
 
    }, { 
 
    title: `speechless`, 
 
    season: 1, 
 
    score: 98, 
 
    }, { 
 
    title: `scherlock`, 
 
    season: 4, 
 
    score: 65, 
 
    }, { 
 
    title: `stranger things`, 
 
    season: 1, 
 
    score: 95, 
 
    }, { 
 
    title: `this is us`, 
 
    season: 1, 
 
    score: 89, 
 
    }, { 
 
    title: `timeless`, 
 
    season: 1, 
 
    score: 84, 
 
    }, { 
 
    title: `the oa`, 
 
    season: 1, 
 
    score: 73, 
 
    }, 
 
]; 
 

 
const wrapWithTag = (content, tagname) => `<${tagname}>${content}</${tagname}>`; 
 

 
const topScoreFilter = show => { 90, 95, 94, 100 }; 
 

 
shows.filter(topScoreFilter); 
 

 
document.write(`<ol>`); 
 
shows.forEach(show => document.write(wrapWithTag(show.title + ` ` + `(` + show.score + `%` + `)`, `li`))); 
 
document.write(`</ol>`);

我如何得到它,根据90的得分超过90与标题会显示和分数不会显示?

+1

如果你使用模板文字,*使用*模板文字(不要做所有这些串联)。 – nnnnnn

回答

4

您可以使用filter

var aboveNinety = shows.filter(show=>show.score > 90); 
+2

由于ES6可用,您也可以使用解构:'shows.filter(({score})=> score> 90)' –

+0

代码不会因为某种原因而工作 –

+0

该代码将查找所有分数高于90但它会返回一个新的数组,它不会将它从'shows'中移除。您的主阵列中仍然会显示所有节目。 – JohanP