0

我正在编写一个代码来收集在电子商务网站上查看的最后一定数量的产品。我可以写得很好,但似乎应该有一种更简单的方法来使用 for 或 while 循环。如果我决定将来需要存储更多产品,那么我必须编写更多代码,并且每个项目的时间都会增加 n+1。这是现在的代码(最多 3 项)

    var url = $.cookie('mycookie');
    var myArray = url.split('»');
    var myArrayLen=myArray.length;

    if (myArrayLen == 1){
          if (url==currentUrl)
            {
                var recentProducts='';
                $.cookie('mycookie', '»'+currentUrl, { path: '/', expires: cookieExp });
            }
            else
            {
                var recentProducts='<div class="mycookie"><h4>Recently Viewed Products</h4>';
                recentProducts += '<a href="' + myArray[1] + '">Link1</a>';
                $.cookie('mycookie', url+'»'+currentUrl, { path: '/', expires: cookieExp });
            }
        }


        else if (myArrayLen == 2){
            if (currentUrl==myArray[1])
            {
                var recentProducts='<div class="mycookie"><h4>Recently Viewed Products</h4>';
                recentProducts += '<a href="' + myArray[2] + '">Link2</a>';
                $.cookie('mycookie', '»'+myArray[2]+'»'+currentUrl, { path: '/', expires: cookieExp });
            }
            else if (currentUrl==myArray[2])
            {
                var recentProducts='<div class="mycookie"><h4>Recently Viewed Products</h4>';
                recentProducts += '<a href="' + myArray[1] + '">Link1</a>';
                $.cookie('mycookie', '»'+myArray[1]+'»'+currentUrl, { path: '/', expires: cookieExp });
            }
            else
            {
                var recentProducts='<div class="mycookie"><h4>Recently Viewed Products</h4>';
                recentProducts += '<a href="' + myArray[1] + '">Link1</a>';
                recentProducts += '<a href="' + myArray[2] + '">Link2</a>';
                $.cookie('mycookie', '»'+myArray[1]+'»'+myArray[2]+'»'+currentUrl, { path: '/', expires: cookieExp });
            }
        } 



        else if (myArrayLen == 3){
            if (currentUrl==myArray[1])
            {
                var recentProducts='<div class="mycookie"><h4>Recently Viewed Products</h4>';
                recentProducts += '<a href="' + myArray[2] + '">Link1</a>';
                recentProducts += '<a href="' + myArray[3] + '">Link2</a>';
                $.cookie('mycookie', '»'+myArray[2]+'»'+myArray[3]+'»'+currentUrl, { path: '/', expires: cookieExp });
            }
            else if (currentUrl==myArray[2])
            {
                var recentProducts='<div class="mycookie"><h4>Recently Viewed Products</h4>';
                recentProducts += '<a href="' + myArray[1] + '">Link1</a>';
                recentProducts += '<a href="' + myArray[3] + '">Link2</a>';
                $.cookie('mycookie', '»'+myArray[1]+'»'+myArray[3]+'»'+currentUrl, { path: '/', expires: cookieExp });
            }
            else if (currentUrl==myArray[3])
            {
                var recentProducts='<div class="mycookie"><h4>Recently Viewed Products</h4>';
                recentProducts += '<a href="' + myArray[1] + '">Link1</a>';
                recentProducts += '<a href="' + myArray[2] + '">Link2</a>';
                $.cookie('mycookie', '»'+myArray[1]+'»'+myArray[2]+'»'+currentUrl, { path: '/', expires: cookieExp });
            }
            else
            {
                var recentProducts='<div class="mycookie"><h4>Recently Viewed Products</h4>';
                recentProducts += '<a href="' + myArray[1] + '">Link1</a>';
                recentProducts += '<a href="' + myArray[2] + '">Link2</a>';
                recentProducts += '<a href="' + myArray[3] + '">Link3</a>';
                $.cookie('mycookie', '»'+myArray[1]+'»'+myArray[2]+'»'+myArray[3]+'»'+currentUrl, { path: '/', expires: cookieExp });
            }
        }
        recentProducts='</div>';

如您所见,1 个项目需要一个 if/else,2 个项目需要一个 if、else if 和 else;3 个项目需要一个 if、else if、else if 和 else。等等。

此脚本正在查看当前 URL 并将其与数组进行比较,如果它在数组中,则不会显示当前 URL,但会将其附加到数组的末尾,因此当您转到下一个时它将显示的项目。如果您有任何问题,请告诉我。非常感谢您的帮助!

4

1 回答 1

-1

我建议您执行以下操作

if (myArrayLen >= 1){
    var index = myArray.indexOf(currentUrl);
    if(index > -1)
        myArray.splice(index,1); //removes currentUrl from myArray
    var recentProducts='<div class="mycookie"><h4>Recently Viewed Products</h4>';
    var cook = '';
    //create links for the rest of the items in the loop.
    for(var i=1;i<myArray.length;i++) {
        recentProducts += '<a href="' + myArray[i] + '">Link'+ i +'</a>';
        cook += '»'+myArray[i];
    }
    recentProducts += '</div>';
    $.cookie('mycookie', cook+'»'+currentUrl, { path: '/', expires: cookieExp });
}
于 2013-11-13T21:37:46.973 回答