字符串&数组操作

1.判断一个字符串中出现次数最多的字符,统计这个次数

var str = 'absdfdfsdfsdf';
function aa(str){
    var obj = {}, key = 0;
    for(var i=0; i<str.length; i++){
        key = str.charAt(i);
        if(obj[key]){
            obj[key]++;
        }else{
            obj[key] = 1;
        }
    }
    var m = 0, i = 0;
    for(var key in obj){
        if(obj[key] > m){
            m = obj[key];
            i = key;
        }
    }
    console.log(i , m)
}

 

2.截取字符串abcdefg的efg

var str = "abcdefg";
var idx =
str.indexOf("efg");

var efg = str.substr(str.indexOf("efg"), 3);
alert(efg);

 

3.数组去重

var array = ['a','b','c','d','e','d','i','d'];
var newAarray = [];
for(var i=0; i<array.length; i++){
    if(newAarray.indexOf(array[i]) == -1){
        newAarray.push(array[i]);
    }
}
console.log(newAarray)

 

posted @ 2015-04-23 11:43  vivi.wang  阅读(210)  评论(0编辑  收藏  举报