// var str = "hello world"; function test(str) { var a = str.replace(/\s/,""); var b = a.split(""); var c = Array.from(new Set(b)); let temp = []; c.map((item,index)=>{ let num = 0; let Index = b.indexOf(item); b.forEach(Item=>{ if (Index !== -1) { num++; Index = b.indexOf(item, Index + 1) } else { return false; } }) let obj= "{"+item+":"+num+"}" temp.push(obj); }) console.log(temp) } // test("hello world"); test.apply(this,["hello world"])