简体   繁体   English

JavaScript 箭头函数和一个 For 循环

[英]JavaScript Arrow Functions and a For Loop

I have a JavaScript function that converts a numeric rating like 3/5 to a visual rating using star HTML entities:我有一个 JavaScript function,它使用星号 HTML 实体将数字评级(如3/5 )转换为视觉评级:

★ ★ ★ ☆ ☆ ★★★☆☆

Here is my function:这是我的 function:

function ratings(rating, total)
{
    var stars = '';
    for(var i = 0; i < total; i++)
    {
        if(i < rating) 
        {
            stars += ' &starf; ';
        }
        else 
        {
            stars += ' &star; ';
        }   
    }
    return stars;        
}

document.write(ratings(3, 5 ));

I want to convert this to an arrow function. Here is what I have so far:我想将其转换为箭头 function。这是我目前所拥有的:

const ratings = (rating, total) => {  
    let stars;
    for(var i = 0; i < total; i++)
    {
        if(i < rating) 
        {
            stars += ' &starf; ';
        }
        else 
        {
            stars += ' &star; ';
        }   
    }
    return stars;  
};

document.write(ratings(3, 5 ));

I've seen some nice clean arrow function examples using map , forEach , and filter to loop through an array.我看到一些漂亮干净的箭头 function 示例使用mapforEachfilter循环遍历数组。 Is there anything similar that would work with my function?有什么类似的东西可以与我的 function 一起使用吗? Not looping through an array, but looping x number of times.不是循环数组,而是循环 x 次。

You could just not iterate at all to be even faster and better to read:你可以根本不迭代来更快更好地阅读:

 const ratings = (rating, total) => ' &starf; '.repeat(rating) + ' &star; '.repeat(total-rating); document.write(ratings(3, 5));

Or something like that...或类似的东西...

 const ratings = (rating, length) => Array.from({length},(_,i) => (i < rating)? '&starf;': '&star;').join(' '); document.write(ratings(3, 5 ));

You can use the methode Array.from to create / initialize your first array.您可以使用Array.from方法来创建/初始化您的第一个数组。 Then you can map through this array to create your rating.然后你可以通过这个数组 map 来创建你的评分。 Don't forget to join the result to get a string as a result.不要忘记join结果以获得字符串作为结果。

const ratings = (rating, total) => Array.from({length: total})
   .map((star, i) => (i < rating) ? ' &starf; ' : ' &star; ')
   .join('');
document.write(ratings(3, 5 ));

Your question is not about arrow functions per say, as you already have one, but rather how to think in terms of arrays and their methods.您的问题不是每个人所说的箭头函数,因为您已经有了一个,而是如何根据 arrays 及其方法进行思考。

First we create an array of total length (1), as that will always be the numbers of stars irrespective of the type.首先,我们创建一个total长度为 (1) 的数组,因为无论类型如何,它始终是星星的数量。 This is a sparse array, so we need to .fill("whatever") it to become something we can map .这是一个稀疏数组,所以我们需要.fill("whatever")它成为我们可以的东西map

Then, each item is mapped to a corresponding star depending on whether its index is less than or equal to rating (2)然后,根据其索引是否小于或等于评级(2),将每个项目映射到相应的星号

Notice the _ to ignore the actual item as we only care about the index.注意_忽略实际项目,因为我们只关心索引。

This gives us the desired array of stars and all we have to do is join(" ") them to have a proper string we can put in the document.这为我们提供了所需的星星数组,我们所要做的就是join(" ")它们以获得我们可以放入文档中的适当字符串。 (3) (3)

 const ratings = (rating, total) => { return Array(total) // 1.fill("").map((_, i) => i < rating? "&starf;": "&star;") // 2.join(" ") // 3 }; document.write(ratings(3, 5 ));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM