Counting the frequency of elements in an array in JavaScript - Stack Overflow

how do I count the frequency of the elements in the array, I'm new to Javascript and pletely lost,

how do I count the frequency of the elements in the array, I'm new to Javascript and pletely lost, I have looked at other answers here but can't get them to work for me. Any help is much appreciated.

function getText() {
    var userText;
    userText = document.InputForm.MyTextBox.value; //get text as string
    alphaOnly(userText);
}

function alphaOnly(userText) {
    var nuText = userText;
    //result = nuText.split("");
    var alphaCheck = /[a-zA-Z]/g; //using RegExp create variable to have only      alphabetic characters
    var alphaResult = nuText.match(alphaCheck); //get object with only alphabetic matches from  original string
    alphaResult.sort();
    var result = freqLet(alphaResult);
    document.write(countlist);
}


function freqLet(alphaResult) {
    count = 0;
    countlist = {
        alphaResult: count
    };
    for (i = 0; i < alphaResult.length; i++) {
        if (alphaResult[i] in alphaResult)
            count[i] ++;
    }
    return countlist;
}

how do I count the frequency of the elements in the array, I'm new to Javascript and pletely lost, I have looked at other answers here but can't get them to work for me. Any help is much appreciated.

function getText() {
    var userText;
    userText = document.InputForm.MyTextBox.value; //get text as string
    alphaOnly(userText);
}

function alphaOnly(userText) {
    var nuText = userText;
    //result = nuText.split("");
    var alphaCheck = /[a-zA-Z]/g; //using RegExp create variable to have only      alphabetic characters
    var alphaResult = nuText.match(alphaCheck); //get object with only alphabetic matches from  original string
    alphaResult.sort();
    var result = freqLet(alphaResult);
    document.write(countlist);
}


function freqLet(alphaResult) {
    count = 0;
    countlist = {
        alphaResult: count
    };
    for (i = 0; i < alphaResult.length; i++) {
        if (alphaResult[i] in alphaResult)
            count[i] ++;
    }
    return countlist;
}
Share Improve this question edited Dec 7, 2014 at 17:57 tsnorri 2,1075 gold badges22 silver badges30 bronze badges asked Dec 7, 2014 at 17:35 ConfuddledConfuddled 351 silver badge5 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 4

To count frequencies you should use an object which properties correspond to the letters occurring in your input string. Also before incrementing the value of the property you should previously check whether this property exists or not.

function freqLet (alphaResult) {
  var count = {};
  countlist = {alphaResult:count};
  for (i = 0; i < alphaResult.length; i++) {
    var character = alphaResult.charAt(i);
    if (count[character]) {
       count[character]++;
    } else {
       count[character] = 1;
    }
  }
  return countlist;
}

If you can use a third party library, underscore.js provides a function "countBy" that does pretty much exactly what you want.

_.countBy(userText, function(character) {
  return character;
});

This should return an associative array of characters in the collection mapped to a count.

Then you could filter the keys of that object to the limited character set you need, again, using underscore or whatever method you like.

Do as below:

var __arr = [6,7,1,2,3,3,4,5,5,5]

function __freq(__arr){
    var a = [], b = [], prev 
    __arr.sort((a,b)=>{return a- b} )

for(let i = 0; i<__arr.length; i++){
    if(__arr[i] !== prev){
        a.push(__arr[i])
        b.push(1)
    }else{
        b[b.length - 1]++
    }
    prev = __arr[i]
}
return [a , b]
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745104295a4611465.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信