Javascript-форум (https://javascript.ru/forum/)
-   Элементы интерфейса (https://javascript.ru/forum/dom-window/)
-   -   как сократить код? (https://javascript.ru/forum/dom-window/74002-kak-sokratit-kod.html)

денис77447327 05.06.2018 10:40

как сократить код?
 
есть скрипт который перходит по ссылкам из массива arr4 рандомно.
вопрос в том как сократить код? чтобы он не занимал столько строк?

var arr4= ["https://site.ru/a-searchlik/d-1/foto-1/p-0/pol-1/spol-2/geo-3159,4312,4400/purp-4096/online-1/bage-18/tage-28/country-3159/region-4312/city-4400",
               "https://site.ru/a-searchlik/d-1/foto-1/p-0/pol-1/spol-2/geo-3159,4312,4400/purp-128/online-1/bage-18/tage-28/country-3159/region-4312/city-4400/",
               "https://site.ru/a-searchlik/d-1/foto-1/p-0/pol-1/spol-2/geo-3159,4312,4400/purp-2/online-1/bage-18/tage-28/country-3159/region-4312/city-4400",
               "https://site.ru/a-searchlik/d-1/foto-1/p-0/pol-1/spol-2/geo-3159,4312,4400/purp-2/online-1/bage-18/tage-28/country-3159/region-4312/city-4400",
               "https://site.ru/a-searchlik/d-1/foto-1/p-0/pol-1/spol-2/geo-3159,4312,4400/purp-0/online-1/bage-18/tage-28/country-3159/region-4312/city-4400",
               "https://site.ru/a-searchlik/d-1/foto-1/p-0/pol-1/spol-2/geo-3159,4312,0/purp-4096/online-1/bage-18/tage-28/country-3159/region-4312/city-0",
               "https://site.ru/a-searchlik/d-1/foto-1/p-0/pol-1/spol-2/geo-3159,4312,0/purp-128/online-1/bage-18/tage-28/country-3159/region-4312/city-0/",
               "https://site.ru/a-searchlik/d-1/foto-1/p-0/pol-1/spol-2/geo-3159,4312,0/purp-2/online-1/bage-18/tage-28/country-3159/region-4312/city-0",
               "https://site.ru/a-searchlik/d-1/foto-1/p-0/pol-1/spol-2/geo-3159,4312,0/purp-2/online-1/bage-18/tage-28/country-3159/region-4312/city-0",
               "https://site.ru/a-searchlik/d-1/foto-1/p-0/pol-1/spol-2/geo-3159,4312,0/purp-0/online-1/bage-18/tage-28/country-3159/region-4312/city-0",
               "https://site.ru/a-searchlik/d-1/foto-1/p-0/pol-1/spol-2/geo-3159,4312,0/purp-4096/bage-18/tage-28/country-3159/region-4312/city-0",
               "https://site.ru/a-searchlik/d-1/foto-1/p-0/pol-1/spol-2/geo-3159,4312,0/purp-128/bage-18/tage-28/country-3159/region-4312/city-0/",
               "https://site.ru/a-searchlik/d-1/foto-1/p-0/pol-1/spol-2/geo-3159,4312,0/purp-2/bage-18/tage-28/country-3159/region-4312/city-0",
               "https://site.ru/a-searchlik/d-1/foto-1/p-0/pol-1/spol-2/geo-3159,4312,0/purp-2/bage-18/tage-28/country-3159/region-4312/city-0",
               "https://site.ru/a-searchlik/d-1/foto-1/p-0/pol-1/spol-2/geo-3159,4312,0/purp-0/bage-18/tage-28/country-3159/region-4312/city-0",
               "https://site.ru/a-searchlik/d-1/foto-1/p-0/pol-1/spol-2/geo-3159,4312,4400/purp-4096/online-1/bage-28/tage-34/country-3159/region-4312/city-4400",
               "https://site.ru/a-searchlik/d-1/foto-1/p-0/pol-1/spol-2/geo-3159,4312,4400/purp-128/online-1/bage-28/tage-34/country-3159/region-4312/city-4400/",
               "https://site.ru/a-searchlik/d-1/foto-1/p-0/pol-1/spol-2/geo-3159,4312,4400/purp-2/online-1/bage-28/tage-34/country-3159/region-4312/city-4400",
               "https://site.ru/a-searchlik/d-1/foto-1/p-0/pol-1/spol-2/geo-3159,4312,4400/purp-2/online-1/bage-28/tage-34/country-3159/region-4312/city-4400",
               "https://site.ru/a-searchlik/d-1/foto-1/p-0/pol-1/spol-2/geo-3159,4312,4400/purp-0/online-1/bage-28/tage-34/country-3159/region-4312/city-4400",
               "https://site.ru/a-searchlik/d-1/foto-1/p-0/pol-1/spol-2/geo-3159,4312,0/purp-4096/online-1/bage-28/tage-34/country-3159/region-4312/city-0",
               "https://site.ru/a-searchlik/d-1/foto-1/p-0/pol-1/spol-2/geo-3159,4312,0/purp-128/online-1/bage-28/tage-34/country-3159/region-4312/city-0/",
               "https://site.ru/a-searchlik/d-1/foto-1/p-0/pol-1/spol-2/geo-3159,4312,0/purp-2/online-1/bage-28/tage-34/country-3159/region-4312/city-0",
               "https://site.ru/a-searchlik/d-1/foto-1/p-0/pol-1/spol-2/geo-3159,4312,0/purp-2/online-1/bage-28/tage-34/country-3159/region-4312/city-0",
               "https://site.ru/a-searchlik/d-1/foto-1/p-0/pol-1/spol-2/geo-3159,4312,0/purp-0/online-1/bage-28/tage-34/country-3159/region-4312/city-0",
               "https://site.ru/a-searchlik/d-1/foto-1/p-0/pol-1/spol-2/geo-3159,4312,0/purp-4096/bage-28/tage-34/country-3159/region-4312/city-0",
               "https://site.ru/a-searchlik/d-1/foto-1/p-0/pol-1/spol-2/geo-3159,4312,0/purp-128/bage-28/tage-34/country-3159/region-4312/city-0/",
               "https://site.ru/a-searchlik/d-1/foto-1/p-0/pol-1/spol-2/geo-3159,4312,0/purp-2/bage-28/tage-34/country-3159/region-4312/city-0",
               "https://site.ru/a-searchlik/d-1/foto-1/p-0/pol-1/spol-2/geo-3159,4312,0/purp-2/bage-28/tage-34/country-3159/region-4312/city-0",
               "https://site.ru/a-searchlik/d-1/foto-1/p-0/pol-1/spol-2/geo-3159,4312,0/purp-0/bage-28/tage-34/country-3159/region-4312/city-0"];
    var rand = Math.floor(Math.random() * arr4.length);

j0hnik 05.06.2018 10:44

денис77447327,
записать в одну строку, а еще по удалять дубли, они тут есть.

денис77447327 05.06.2018 11:54

с дублями согласен, а как в одну строку можно пример?

j0hnik 05.06.2018 11:58

var arr4= ['...'], rand = Math.floor(Math.random() * arr4.length);

денис77447327 05.06.2018 12:04

вы не так поняли я имел ввиду, как из нескольких ссылок разных из массива сделать одну универсальную. чтобы при переходе по ней в ней каждый раз менялись значения рандомно

j0hnik 05.06.2018 12:12

рандомно? хм..
если в ссылке указать абракадабру и перейти по ней будет такое работать?

денис77447327 05.06.2018 12:25

Цитата:

Сообщение от j0hnik (Сообщение 486688)
рандомно? хм..
если в ссылке указать абракадабру и перейти по ней будет такое работать?

рандомно в рамках значений ссылок массива

j0hnik 05.06.2018 12:34

var arr1 = ['geo-3159,4312,4400/', 'geo-3159,4312,0/'],
arr2 = ['purp-2','purp-128/'],
///..... и так для каждого уровня вложенности
url  = 'https://site.ru/a-searchlik/d-1/foto-1/p-0/pol-1/spol-2/'+arr1[Math.floor(Math.random() * arr1.length)]+arr2[Math.floor(Math.random() * arr2.length)]//... и так для каждого уровня

денис77447327 05.06.2018 13:09

спасибо


Часовой пояс GMT +3, время: 10:17.