Following one of the answers of How to count certain elements in array? created the JavaScript code below to build a Object, what can be used to build a Object named chars what can be used as chars.str
and chars.num
:
const arr="abrakadabra".split('');
const counts = {};
arr.forEach((el) => {
counts[el] = counts[el] ? (counts[el] += 1) : 1;
});
const countsSorted = Object.entries(counts).sort(([_, b], [__, a]) => a - b);
console.log(countsSorted); // [["a",5],["b",2],["r",2],["k",1],["d",1]]
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
What is the elegant way to create Object from countsSorted
what can be used as chars.str
and chars.num
, like:
Object {
num: [5, 2, 2, 1, 1],
str: ["a", "b", "r", "k", "d"]
}