【问题标题】:Compare the first word of a page title to an array in Javascript将页面标题的第一个单词与 Javascript 中的数组进行比较
【发布时间】:2020-09-19 19:49:42
【问题描述】:

以下代码获取页面标题,将其与数组中的数据进行比较并输出最终值

var title = (document.title);

//test variables 
testarray =["blue","top","110","in stock", "red","down","111","in stock"]

//function
function testfunction(array, variable){
    var varindex = array.indexOf(variable)
    return array[varindex+2] 
}
//calling the function
testfunction(testarray, title)

var finalvalue = testfunction(testarray, title);

因此,如果我的页面标题是蓝色,它会给出我需要的值 110。 它工作正常,但这样我页面的唯一有效标题必须是 Blue,否则它将无法工作。

我希望能够拥有更长的页面标题,例如 Blue Shoes 。我尝试在开头添加以下变量以仅获取标题第一个单词

var fulltitle = (document.title);
var title = fulltitle.split(' ').slice(0,1);

但它不起作用。我的代码有什么问题? 谢谢

【问题讨论】:

  • 试试 `title = fulltitle.trim().split(' ')[0]'
  • @Francesco 你可以检查我的答案,因为它通过了大部分测试用例。

标签: javascript arrays variables split title


【解决方案1】:

我不确定我是否理解正确,但可能存在一个问题。数组中的标题和值可以从大写或小写开始。尝试做这样的事情。

var title = (document.title);

//test variables 
testarray =["blue","top","110","in stock", "red","down","111","in stock"]

//function
function testfunction(array, variable){
    // CHANGES HERE
    var varindex = array.indexOf(variable.toLocaleLowerCase())
    return array[varindex+2] 
}
//calling the function
testfunction(testarray, title)

var finalvalue = testfunction(testarray, title);

【讨论】:

    【解决方案2】:

    这里有几个问题:

    1. document.title蓝鞋,然后你这样做:

      fulltitle.split(' ').slice(0,1)
      

      它实际上返回一个类似["Blue"] 的数组。您需要先将其转换为文本,例如:

      var title = fulltitle.split(' ').slice(0,1)[0];
      
    2. 此外,返回的 title 值与 testarray 数组变量 ("Blue" !== "blue") 中的值不同,因此 indexOf 不起作用。您需要将标题设为小写,例如:

      var varindex = array.indexOf(variable.toLowerCase())
      

    工作演示:

    var fulltitle = "Blue Shoes";
    var arr = fulltitle.split(' ').slice(0, 1);
    var title = arr && arr.length ? arr[0] : "";
    
    //test variables 
    testarray = ["blue", "top", "110", "in stock", "red", "down", "111", "in stock"]
    
    //function
    function testfunction(array, variable) {
      var varindex = array.indexOf(variable.toLowerCase())
      return array[varindex + 2]
    }
    
    //calling the function
    var finalvalue = testfunction(testarray, title);
    
    console.log( finalvalue )

    【讨论】:

      【解决方案3】:

      它成功的测试用例在数组中包含blueblueberryberry blue等词。
      只需用这个

      替换您的测试功能
      function testfunction(array, variable){
         let indexCaptured = '';
         array.forEach((res, index) => {
            if(res.includes(variable)) {
               indexCaptured = array[index+2] 
            }
         });
         return indexCaptured;
      }
      

      【讨论】:

        【解决方案4】:

        试试var title = fulltitle.split(' ')[0]。并将比较的部分带到同一个寄存器var varindex = array.indexOf(variable.toLowerCase())

        【讨论】:

          【解决方案5】:

          使用findIndex()startsWith()

          var title = 'red title';
          
          //test variables 
          testarray =["blue","top","110","in stock", "red","down","111","in stock"]
          
          //function
          function testfunction(array, variable){
              var varindex = array.findIndex(e => variable.startsWith(e))
              return array[varindex+2] 
          }
          //calling the function
          testfunction(testarray, title)
          
          var finalvalue = testfunction(testarray, title);
          console.log(finalvalue)

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2022-12-05
            • 2021-03-11
            • 1970-01-01
            • 2020-04-15
            • 1970-01-01
            • 2014-10-19
            • 1970-01-01
            相关资源
            最近更新 更多