需要了解如何实现它

时间:2016-10-26 05:02:07

标签: javascript jquery angularjs

你能否给我一个想法可能正在使用javascriptjqueryangular

我正在生成三个随机数组 例如:

(This values are generating using random array =['member', 'medical', 'rx', 'disability'];)

输出

member      disability  medical     disability

medical     member      disability  rx

disability  disability  medical     member

disability  member      member      disability

如何计算所有列中的值和最大值是否出现 例如,残疾就是答案 感谢

1 个答案:

答案 0 :(得分:1)

在JS中,您可以使用JSON来计算数组中单词出现的次数:

var countArr = [
    {"word":"member", "occur":0},
    {"word":"disability ", "occur":0}
];

var All = random1.concat(random2).concat(random3);

for(i = 0; i < All.length; i++){
    count(All[i]);
}

function count(word){
    var x = countArr.findIndex(function(elm){ return elm.word == word;});
    if(x != -1){
         ++countArr[x].occur;
    }
    else{
         countArr.push({"word":word, "occur":0});
    }
}

您可以轻松地向JSON添加有关您单词的更多详细信息,例如数组中出现单词的索引。