在$ .each()中混洗多个数组

时间:2015-09-15 12:42:13

标签: jquery

我正在以下列方式在para中生成超链接:

<p id="category1"></p>

jQuery代码:

$(function () {
    var link = [
        ["category1", "http://www.xyzabcxyz.com/apple", "Apple description comes here"],
        ["category2", "http://www.xyzabcxyz.com/banana", "Banana description comes here"],
        ["category3", "http://www.xyzabcxyz.com/apricots", "Apricots description comes here"],
        ["category4", "http://www.xyzabcxyz.com/peaches", "Peaches description comes here"]
    ];
    $.each(link, function (e) {
       if ($("#" + link[e][0])[0]) {
           $("#" + link[e][0]).append('<a target="_blank" href="' + link[e][1] +
                       '">' + link[e][2] + '</a>');
       }
    });
});

演示:http://jsfiddle.net/j2g411yk/

到目前为止一切顺利。一切正常。

我想知道如何更改我的代码,以便随机抽取一个类别中的多个产品。像这样:

$(function () {
    var link = [
        [["category1", "http://www.xyzabcxyz.com/apple", "Apple description comes here"],
                       "http://www.xyzabcxyz.com/pineapple", "Pineapple description comes here"],
                       "http://www.xyzabcxyz.com/lemon", "Lemon description comes here"]],
        ["category2",  "http://www.xyzabcxyz.com/banana", "Banana description comes here"],
        [["category3", "http://www.xyzabcxyz.com/apricots", "Apricots description comes here"],
                       "http://www.xyzabcxyz.com/Berries", "Berries description comes here"]]
        ["category4", "http://www.xyzabcxyz.com/peaches", "Peaches description comes here"]
    ];
    $.each(link, function (e) {
       if ($("#" + link[e][0])[0]) {
           $("#" + link[e][0]).append('<a target="_blank" href="' + link[e][1] +
                       '">' + link[e][2] + '</a>');
       }
    });
});

因此,对于一个用户,它可能会显示关于Apple的超链接,而对于另一个用户,它可能是Lemon的超链接。如果同一访问者刷新页面,则应显示相同类别的新产品。

P.S:我想要一个随机链接,但不是我必须使用cookie来保持跟踪,如果访客A已经看到该链接。同一个用户可能会在刷新时看到相同的产品两次,这是完全可以的。

2 个答案:

答案 0 :(得分:0)

您可以使用math.random()从类别数组中获取随机链接,并使用cookie或localStorage来跟踪已经看到的链接。

答案 1 :(得分:0)

试试这个:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <script type='text/javascript' src='js/jquery.min.js'></script>
</head>
<body>
    <p id="category1"></p>
    <p id="category4"></p>
    <script>
        function getRandomInt (min, max) {
            if (max == 0) return 0;
            return Math.floor(Math.random() * (max - min + 1)) + min;
        } 

        $(function () {
            var link = [
                ["category1", [[ "http://www.xyzabcxyz.com/apple", "Apple description comes here"],
                               ["http://www.xyzabcxyz.com/pineapple", "Pineapple description comes here"],
                               ["http://www.xyzabcxyz.com/lemon", "Lemon description comes here"]]],
                ["category2", [["http://www.xyzabcxyz.com/banana", "Banana description comes here"]]],
                ["category3", [["http://www.xyzabcxyz.com/apricots", "Apricots description comes here"],
                               ["http://www.xyzabcxyz.com/Berries", "Berries description comes here"]]],
                ["category4", [["http://www.xyzabcxyz.com/peaches", "Peaches description comes here"]]]
            ];
            $.each(link, function (e,v) {
                if ($("#" + v[0]).length) {
                    var min = 0; 
                    var max = (v[1].length)-1;
                    var i = getRandomInt(min,max);

                    $("#" + v[0]).append('<a target="_blank" href="' + v[1][i][0] +'">' + v[1][i][1] + '</a>');
                }
            });
        });
    </script>   
</body>

对于随机函数tks。到
Javascript: Generate a random number within a range using crypto.getRandomValues