Javascript 如何获取数组中最频繁的项(数字或字符串)?
我正在尝试获取由Dialogflow聊天机器人使用fullfilment代码构建的javascript数组中最频繁的项。 但是,如果我可以显示数组,我尝试的函数似乎无法很好地查找最频繁的项:Javascript 如何获取数组中最频繁的项(数字或字符串)?,javascript,arrays,frequency,Javascript,Arrays,Frequency,我正在尝试获取由Dialogflow聊天机器人使用fullfilment代码构建的javascript数组中最频繁的项。 但是,如果我可以显示数组,我尝试的函数似乎无法很好地查找最频繁的项: const functions = require('firebase-functions'); const {WebhookClient} = require('dialogflow-fulfillment'); const {Card, Suggestion} = require('dialogflow
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
var answers = [];
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
function welcome(agent) {
agent.add(`Welcome to my agent!`);
}
function fallback(agent) {
agent.add(`I didn't understand FULLFILMENT`);
agent.add(`I'm sorry, can you try again? FULLFILMENT`);
}
function rhymingWordHandler(agent){
agent.add('Intent called');
}
function answer1Handler(agent){
agent.add('Intent answer1 called');
const answer = agent.parameters.number;
answers.push(answer);
}
function answer2Handler(agent){
agent.add('Intent answer2 called');
const answer = agent.parameters.number;
answers.push(answer);
}
function answer3Handler(agent){
agent.add('Intent answer3 called');
const answer = agent.parameters.number;
answers.push(answer);
agent.add('Here is the mode');
const mfi = mode(answers);
agent.add(mfi.toString());
}
function mode(arr1){
var mf = 1; //default maximum frequency
var m = 0; //counter
var item; //to store item with maximum frequency
for (var i=0; i<arr1.length; i++) //select element (current element)
{
for (var j=i; j<arr1.length; j++) //loop through next elements in array to compare calculate frequency of current element
{
if (arr1[i] == arr1[j]) //see if element occurs again in the array
m++; //increment counter if it does
if (mf<m) //compare current items frequency with maximum frequency
{
mf=m; //if m>mf store m in mf for upcoming elements
item = arr1[i]; // store the current element.
}
}
m=0; // make counter 0 for next element.
}
return item;
}
// Run the proper function handler based on the matched Dialogflow intent name
let intentMap = new Map();
intentMap.set('Default Fallback Intent', fallback);
intentMap.set('RhymingWord', rhymingWordHandler);
intentMap.set('answer1', answer1Handler);
intentMap.set('answer2', answer2Handler);
intentMap.set('answer3', answer3Handler);
agent.handleRequest(intentMap);
});
const functions=require('firebase-functions');
const{WebhookClient}=require('dialogflow-fulfillment');
const{Card,Suggestion}=require('dialogflow-fulfillment');
var回答=[];
process.env.DEBUG='dialogflow:DEBUG';//启用lib调试语句
exports.dialogflowFirebaseFulfillment=functions.https.onRequest((请求,响应)=>{
const-agent=new-WebhookClient({request,response});
log('Dialogflow请求头:'+JSON.stringify(Request.headers));
log('Dialogflow请求主体:'+JSON.stringify(Request.body));
功能欢迎(代理){
agent.add(`Welcometomyagent!`);
}
功能回退(代理){
agent.add(‘我不懂FULLFILMENT’);
agent.add(`对不起,你能再试一次吗?FULLFILMENT`);
}
函数rhymingWordHandler(代理){
add('Intent called');
}
功能应答器1处理程序(代理){
agent.add('Intent answer1 called');
const answer=agent.parameters.number;
回答。推(回答);
}
函数应答器2处理程序(代理){
agent.add('Intent answer2 called');
const answer=agent.parameters.number;
回答。推(回答);
}
职能负责人3管理人(代理人){
agent.add('Intent answer3 called');
const answer=agent.parameters.number;
回答。推(回答);
add('这是模式');
常量mfi=模式(答案);
agent.add(mfi.toString());
}
功能模式(arr1){
var mf=1;//默认最大频率
var m=0;//计数器
var item;//以最大频率存储项
对于(var i=0;i,它应该是正确的。类似的东西应该适用于您的模式
函数:
function mode(arr) {
const counts = {};
let maxCount = 0;
let maxKey;
// Count how many times each object (or really its string representation)
// appears, and keep track of the highest count we've seen.
for (let i = 0; i < arr.length; i++) {
const key = arr[i];
const count = (counts[key] = (counts[key] || 0) + 1);
if (count > maxCount) {
maxCount = count;
maxKey = key;
}
}
// Return (one of) the highest keys we've seen, or undefined.
return maxKey;
}
功能模式(arr){
常量计数={};
设maxCount=0;
让maxKey;
//计算每个对象(或其字符串表示形式)的次数
//出现,并跟踪我们见过的最高计数。
for(设i=0;i最大计数){
最大计数=计数;
maxKey=key;
}
}
//返回(其中一个)我们见过的最高键,或未定义。
返回maxKey;
}
使用全局answers
数组会让你很不愉快;它将在你的机器人的所有客户端之间共享。除非你想这样做,当然…@AKX Ho,不。当然不是。我希望每个客户端都有自己的特点