简体   繁体   English

如何使用Javascript匹配不同数组中的字符串

[英]How to match strings in different arrays using Javascript

I'm creating a script in Google Optimize for a travel website.我正在 Google Optimize 中为旅游网站创建脚本。 The idea is to return random strings from an array in order to display a tip in the search bar, eg "Try searching for [location]".这个想法是从数组中返回随机字符串,以便在搜索栏中显示提示,例如“尝试搜索 [位置]”。 However, there are two search bars;但是,有两个搜索栏; if you click on the first one it unfolds and a second one appears.如果您单击第一个,它会展开并出现第二个。

Right now, the same line is displayed in both search bars.现在,同一行显示在两个搜索栏中。

Rather than one array that contains both the destinations and hotel names I would like to create two arrays, one with the destinations and one with the hotel names.我想创建两个数组,一个包含目的地,一个包含酒店名称,而不是一个包含目的地和酒店名称的数组。 Is there a way to link each string to another one from a different array, for example by giving them values.有没有办法将每个字符串链接到来自不同数组的另一个字符串,例如通过给它们值。

Try searching for Spain and then in the second search bar try [Spanish hotel].尝试搜索西班牙,然后在第二个搜索栏中尝试 [西班牙酒店]。

Check the screenshot below检查下面的屏幕截图在此处输入图片说明

 $(".vak-field__cover:first").on("click", function(){ var texts = ["Egypt", "Spain", "Creta", "France", "Fuerteventura", "Bali", "Sicily", "Turkish Riviera", "Mallorca", "Gran Canaria", "Mirador Maspalomas", "Vila Sal Azul", "Millor paradiso playa", "Cinc plats *3", "Portobello village", "Main star park 5*", "Sam's treasure trove"] ; var randomNumber = Math.floor(Math.random() * 17); console.log(randomNumber); $( ".vak-quicksearch__placeholder:first" ).html( "You could search for \\"" + texts[randomNumber] + "\\"" ); $( ".vak-field>input" ).attr( "placeholder", "Try \\"" + texts[randomNumber] + "\\"" ); });

It makes little sense having two, very disparate forms of data (countries and hotels) in one array.在一个数组中包含两种非常不同形式的数据(国家和酒店)毫无意义。

Instead you need a data model that relates these two things.相反,您需要一个将这两件事联系起来的数据模型。

Something like:就像是:

//prep nested, relational data
let data = [
    {
        name: 'Egypt',
        hotels: [
            'Egypt hotel 1',
            'Egypt hotel 2'
        ]
    },
    {
        name: 'Spain',
        hotels: [
            'Spain hotel 1',
            'Spain hotel 2'
            /* etc */
        ]
    },
];

//establish random location and hotel of that location
let rand_location = data[Math.floor(Math.random() * data.length)];
let rand_hotel = rand_location.hotels[Math.floor(Math.random() * rand_location.hotels.length)];

//output
$( ".vak-quicksearch__placeholder:first" ).html( "You could search for \"" + rand_location.name  + "\"" );
$( ".vak-field>input" ).attr( "placeholder", "Try \"" + rand_hotel + "\"" );

Also note I made dyanmic your random generator;另请注意,我为您的随机生成器设置了动态; you hard-coded it to 17 (the number of items in your array);您将其硬编码为 17(数组中的项目数); better is to have it read from the genuine length of the array, so your code doesn't break when you add to or remove items from your data.更好的是让它从数组的真实长度中读取,这样当您从数据中添加或删除项目时,您的代码就不会中断。

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

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