Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将字符串参数与发送给方法的最后100个字符串进行比较的TypeScript方法_Javascript_Node.js_Typescript_Collections - Fatal编程技术网

Javascript 将字符串参数与发送给方法的最后100个字符串进行比较的TypeScript方法

Javascript 将字符串参数与发送给方法的最后100个字符串进行比较的TypeScript方法,javascript,node.js,typescript,collections,Javascript,Node.js,Typescript,Collections,我需要在Node.js(v8.9.1)中运行的TypeScript(v2.6.1)中的类中创建一个方法,该类接受一个字符串,将作为参数发送的字符串与发送给该方法的最后100个字符串进行比较。如果应用程序重新启动,则无需在内存中保留字符串。我的想法是这样的: class StringService { private last100Strings; //What type? constructor() { } checkIfIsInLast100Stri

我需要在Node.js(v8.9.1)中运行的TypeScript(v2.6.1)中的类中创建一个方法,该类接受一个字符串,将作为参数发送的字符串与发送给该方法的最后100个字符串进行比较。如果应用程序重新启动,则无需在内存中保留字符串。我的想法是这样的:

class StringService {

    private last100Strings;    //What type?

    constructor() {
    }

    checkIfIsInLast100Strings(string : string) : boolean {
        //check if string is in the last 100 strings checked
        //if yes 
            //return true
        //if no
            //add the string to last100strings
            //check if the last100strings contains more than 100 strings
            //if yes
                //remove the oldest string
            //return false
    }
}
我最初考虑将set用于.has方法,但我找不到从字符串集中删除最旧字符串的方法。相反,我将使用字符串数组实现,但由于该方法将频繁执行,我不确定这是否是最好的方法


关于如何有效地实现这一点,有什么建议吗?

注意:由于这适用于任何数量的字符串,因此我将使用
N
来表示这一点。在您的情况下,
N=100
,但您可以对其进行细微的更改

您只需要保留一组项,因为您只需要在其中插入唯一的字符串。集合保留项目的插入顺序,因此您可以通过获取集合中的“第一个”项目来获取最旧的项目

以下是实现的样子:

类服务{
私有最大大小:数字;
private lastnstring:Set=new Set();
构造函数(maxSize=100){//使最大大小可配置,但默认为100
if(maxSize this.maxSize){//if是
//删除最旧的字符串
const[laster]=this.lastNStrings;//等效于this.lastNStrings[Symbol.iterator]().next().value
this.lastNStrings.delete(最早的);
}
返回错误
}
}
}

据我所知,自2.6版以来,没有任何类型脚本语法会影响这一点。但是,我无法用旧版本测试它。如果有,它们应该是次要的,并且易于修改

下面是代码的JavaScript演示:

类服务{
lastNStrings=新集合();
构造函数(maxSize=100){//使最大大小可配置,但默认为100
if(maxSize this.maxSize){//if是
//删除最旧的字符串
const[laster]=this.lastNStrings;//等效于this.lastNStrings[Symbol.iterator]().next().value
this.lastNStrings.delete(最早的);
}
返回错误
}
}
}
常量服务=新的StringService(4);//最后4点现在:
console.log(service.checkIfIsInLastStrings(“a”);//虚假的;最后4个现在:“a”
console.log(service.checkIfIsInLastStrings(“b”);//虚假的;最后4个现在:“a”,“b”
console.log(service.checkIfIsInLastStrings(“a”);//是的;最后4个现在:“a”,“b”
console.log(service.checkIfIsInLastStrings(“c”);//虚假的;最后4个现在:“a”、“b”、“c”
console.log(service.checkIfIsInLastStrings(“d”);//虚假的;最后4个现在:“a”、“b”、“c”、“d”
console.log(service.checkIfIsInLastStrings(“e”);//虚假的;最后4个现在:“b”,“c”,“d”,“e”
console.log(service.checkIfIsInLastStrings(“b”);//是的;最后4个现在:“b”,“c”,“d”,“e”
console.log(service.checkIfIsInLastStrings(“c”);//是的;最后4个现在:“b”,“c”,“d”,“e”
console.log(service.checkIfIsInLastStrings(“d”);//是的;最后4个现在:“b”,“c”,“d”,“e”
console.log(service.checkIfIsInLastStrings(“e”);//是的;最后4个现在:“b”,“c”,“d”,“e”

console.log(service.checkIfIsInLastStrings(“f”);//虚假的;现在是最后4个:“c”、“d”、“e”、“f”
注意:由于这适用于任何数量的字符串,因此我将使用
N
来表示这一点。在您的情况下,
N=100
,但您可以对其进行细微的更改

您只需要保留一组项,因为您只需要在其中插入唯一的字符串。集合保留项目的插入顺序,因此您可以通过获取集合中的“第一个”项目来获取最旧的项目

以下是实现的样子:

类服务{
私有最大大小:数字;
private lastnstring:Set=new Set();
构造函数(maxSize=100){//使最大大小可配置,但默认为100
if(maxSize this.maxSize){//if是
//删除最旧的字符串
const[laster]=this.lastNStrings;//等效于this.lastNStrings[Symbol.iterator]().next().value
this.lastNStrings.delete(最早的);
}
返回错误
}
}
}

据我所知,自2.6版以来,没有任何类型脚本语法会影响这一点。但是,我无法用旧版本测试它。如果有,它们应该是次要的,并且易于修改

下面是代码的JavaScript演示:

类服务{
lastNStrings=新集合();
构造函数(maxSize=100){//使最大大小可配置,但默认为100
if(maxSize this.maxSize){//if是
//删除最旧的字符串
const[laster]=this.lastNStrings;//等效于this.lastNStrings[Symbol.iterator]().next().value
this.lastNStrings.delete(最早的);
}
返回错误
}
}
}
常量服务=新的StringService(4);//最后4点现在:
console.log(service.checkIfIsInLastStrings(“a”);//虚假的;最后4个现在:“a”
console.log(service.checkIfIsInLastStrings(“b”);//虚假的;最后4个现在:“a”,“b”
console.log(service.checkIfIsInLastStrings(“a”);//是的;最后4个现在:“a”,“b”
console.log(service.checkIfIsInLastStrings(“c”);//虚假的;最后4个现在:“a”、“b”、“c”
console.log(service.checkIfIsInLastStrings(“d”);//虚假的;最后4个现在:“a”、“b”、“c”、“d”
console.log(service.checkIfIsInLastStrings(“e”);//虚假的;最后4个现在:“b”,“c”,“d”,“e”
console.log(service.checkIfIsInLastStrings(“b”);//是的;最后4个现在:“b”,“c”,“d”,“e”
console.log(service.checkIfIsInLastStrings(“c”);//是的;最后4个现在:“b”,“c”,“d”,“e”
console.log(service.checkIfIsInLastStrings(“d”);//是的;最后4个现在:“b”,“c”,“d”,“e”
console.log(service.checkifisinlasst