数组返回indexOf而不是模式计数

Array returning indexOf rather than count of pattern

本文关键字:模式 返回 indexOf 数组      更新时间:2023-09-26

所以我已经编辑了我的代码一点,我卡住了试图计数的次数一个模式出现在一个指定的记录

我希望数组返回为2,1,0,如果搜索中的,但目前我得到的是13,19,-1

我将非常感谢任何关于如何改进我的代码的提示。

books = [
    {
    title: "Inheritance: Inheritance Cycle, Book 4",
    author: "Christopher Paolini",
    },
{
    title: "The Sense of an Ending",
    author: "Julian Barnes"},
{
    title: "Snuff Discworld Novel 39",
    author: "Sir Terry Pratchett",
    }
]
search = prompt("Title?");
function count(books, pattern) {
        var num = 0;
        var result = [];
        for (i = 0; i < books.length; i++) {
            var index = books[i].title.toLowerCase().indexOf(pattern.toLowerCase());
            do {
                result[i] = index;
                index = books[i].title.toLowerCase().indexOf(pattern.toLowerCase(), index + 1);
            }
            while (index >= 0)
            num = 0;
        }
        return result;
    }
alert(count(books, search));

您需要保留计数,而不是在result中找到它的索引。只有当索引大于或等于零时,才应该增加计数。注意,我还做了一些优化,以减少转换字符串所需的次数。我还添加了一个使用正则表达式的不同版本。

function count(books, pattern) {
    var result = [];
    for (i = 0; i < books.length; i++) {
        var index = -1, // set to -1 so that the first iteration will set it to 0
            title = books[i].title.toLowerCase(),
            realPattern = pattern.toLowerCase();
        result[i] = 0;
        do {
            index = title.indexOf(pattern, index + 1);
            if (index >= 0) {
               result[i] = result[i] + 1;
            }
        }
        while (index >= 0)
    }
    return result;
}

更好的选择

RegExp.escape = function(text) {
    return text.replace(/[-[']{}()*+?.,''^$|#'s]/g, "''$&");
}
function count(books, pattern) {
    var result = [];
    for (i = 0; i < books.length; i++) {
        var regex = new RegExp(RegExp.escape(pattern),'gi'),
            matches = books[i].title.match(regex);
        result[i] = matches ? matches.length : 0;
    }
    return result;
}

您可以使用for in循环并将循环变量压入results变量。

jsfiddle

var arr = ['a', 'b', 'c', 'a'];
function count(books, pattern) {   
    var results = [];
    for (var index in arr) {
        if (arr[index] === pattern) {
          results.push(index);
        } 
    }
    return results;
}
console.log(count(arr, 'a'));

试试这个,非常简单:

function count(books, pattern) 
{
var num = 0;
var result = Array();
for (i = 0; i < books.length; i++) 
{
    var times = books[i].title.toLowerCase().split(pattern.toLowerCase());
    if(times.length - 1 >= 0)result[i] = times.length - 1;
}
return result;
}

希望你会发现这是有用的!

使用match可以大大简化代码。我在这里做了一个jsfiddle: http://jsfiddle.net/NTWNn/它看起来像这样:

var books = [
    {
        title: "Inheritance: Inheritance Cycle, Book 4",
        author: "Christopher Paolini",
    },
    {
        title: "The Sense of an Ending",
        author: "Julian Barnes"
    },
    {
        title: "Snuff Discworld Novel 39",
        author: "Sir Terry Pratchett",
    }
]
search = prompt("Title?");
function count(pattern)
{
    var results = [];
    var matches = null;
    pattern = new RegExp(pattern, "g")
    for (i = 0; i < books.length; i++) {
        results[i] = 0;
        matches = books[i].title.toLowerCase().match(pattern);
        if(matches != null) {
            results[i] = matches.length;
        }
    }
    return results
}
alert(count(search))