javascript - array.slice 不起作用,而不是减去一些值,它只是按原样复制数组

标签 javascript slice

这是我在文本中查找单词的小脚本。问题部分在 for 循环中,当切片数组不起作用时。 array.slice 想要取值 (0,6) 并且迭代数组的某些单词比它短,所以我想这可能是个问题。你能给我一个应对这种情况的建议吗?

/*jshint multistr:true */
var text = "Etiam accumsan facilisis blandit. Praesent convallis sapien at sagittis ultrices. Proin adipiscing Michal, dolor at adipiscing volutpat, tellus neque adipiscing risus, at interdum magna ante quis risus. Quisque semper in mi imperdiet interdum. Fusce laoreet leo blandit, convallis tellus ut, posuere mauris michal. Quisque eu pulvinar felis. Maecenas ultricies a nulla et dignissim. Praesent egestas quam massa, eget dignissim nulla fringilla nec. Etiam non nulla imperdiet, tempus erat sit amet, luctus nibh. Aenean sit amet elementum nunc. Duis venenatis lacinia rutrum. Fusce vulputate michal lacinia odio, non laoreet metus placerat id. Nam ac risus et nisl pretium vulputate.";
var myName = "michal";
var hits = [];

var name = myName.toLowerCase();
var text2 = text.toLowerCase();

// changes string into array by space
var textWords = text2.split(" ");

// looks for each array value and compares it with the name
for (e = 0;e <= textWords.length; e++) {
    console.log("______ new iteration -> another word to slice _______");

    //  each time 'e' changes, it turns array into another one, consisting of just a word (+ space or comma or dot)
    var g = e+1;
    var textWord = textWords.slice(e, g);
    console.log(textWord);

    // !! PROBLEM part, JS will not slice the former array so it is just name.length long !!
    var potentialName = textWord.slice(0,name.length);
    console.log(potentialName);

    // pushes the result word into empty array 'hits'
    if (potentialName === name) {
        hits.push(potentialName);
//        console.log(hits);
    }
}

// takes the first value of the result array
var nameCopy = hits.slice(0,1);
// counts the number of result values
var count = hits.length;

// checks if ther is any matching value and how many of them there is
if ((nameCopy === name) && (count > 1)) {
    console.log("I've found your name in the text! It's there " + count + " times.");
} else if (nameCopy === name) {
    console.log("I've found your name in the text once.");
} else {
    console.log("Your name is not in the text.");
}

最佳答案

slice 返回一个数组,这意味着:

  • potentialName 是一个数组,而不是字符串,因此永远不会等于 (===) name
  • textWord也是一个数组,由于g总是e + 1,我们可以进一步推断它总是一个数组包含单个元素,即当前单词。我重复一遍:textWord 是一个包含单词的数组,而不是您所期望的单词本身。

让我们在第一次迭代时从循环的第一行开始逐步运行您的程序:

  • e 为 0。
  • ge + 10 + 1 即 1。
  • textWordtextWords.slice(e, g)['etiam', 'accumsan', ...].slice(0, 1)['etiam'](一个数组)。
  • potentialNametextWord.slice(0, name.length)['etiam'].slice(0, 6)这是 ['etiam']

我猜第三步不是你想要的。我猜你想得到第 e 个词。您需要做的不是 slice 而是 [],就像这样:textWords[e]。使用它,在第一次迭代中,textWord 将是 'etiam' 而不是 ['etiam']。然后您可以安全地将 textWordname 进行比较,并使用 === 查看它们是否相等。字符串不需要具有相同的长度。

希望对您有所帮助。

当然,它们是计算文本中单词数量的更简单方法,但我猜这是一种学习练习,因此可以用于此目的。

关于javascript - array.slice 不起作用,而不是减去一些值,它只是按原样复制数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25406342/

相关文章:

rust - 管理同一内存段的多个切片

arrays - 获取作为参数传入的数组的大小

JavaScript 对象规则

javascript - 在 Google Apps 脚本中使用 Crypto-JS - 什么是 C.lib?

javascript - 在 JQuery 中克隆 li 元素

go - 将数据发送到 channel slice 中的 channel 时出现死锁

pointers - 使用指向值的指针作为 slice

javascript - 无重复的笛卡尔积

javascript - jQuery :not() selector does not work in . off() 方法

string - 如何在go模板中移动 slice ?