JavaScript array to dictionary conversation based on count

javascript map
javascript sum array of objects
sum array javascript
reduce js
javascript reduce array of objects
javascript array filter
javascript sum of numbers
javascript reduce object

I have a array like below

 data = [A,B,B,B,C,C,A,B]

How can I convert into dictionary like below format.

  data = [
    name: "A",
    y: 2
    name: "B",
    y: 4
    name: "C",
    y: 2

Have to convert elements as names and count of the elements as value to y.

I've a library which accepts only in that format. Not able to do, stuck in the middle

Any suggestions are welcome.

Other answers look really nice and good at readability. This is a little efficient code, good if data is too large

function convert(data){
  var myMap = {}
  data.forEach(el => myMap[el] = myMap[el] != undefined ? myMap[el] + 1 : 1);
  return Object.keys(myMap).map(k => {return {name: k, y: myMap[k]}})


Array, The JavaScript Array class is a global object that is used in the Arrays cannot use strings as element indexes (as in an associative array) but must use integers. into a string by the JavaScript engine through an implicit toString conversion. Read only, The zero-based index of the match in the string. 1. On a high level, the above is essentially either finding an existing entry in the accumulator array, and then incrementing the counter property, OR it's short-circuiting to the next expression which will make a brand new entry in the accumulator with count 0, and then increment that count, this way we combine the two expressions and reduce the need for any conditionals.

data = ['A','B','B','B','C','C','A','B'];

var res = Array.from(new Set(data)).map(a =>
  ({name:a, y: data.filter(f => f === a).length}));


Iterating Over and Reducing Data, We can use this to modify the data in place, generate counts, or perform other Instead these nested objects are simply passed by referenced - meaning the original This creates a new array of data (again see transformation talk above) with Similar to filtering, sorting data based on attributes is something you'll want to  JavaScript does not support associative arrays. You should use objects when you want the element names to be strings (text). You should use arrays when you want the element names to be numbers.

use array.reduce:

data = ['A','B','B','B','C','C','A','B'];

var res = data.reduce((m, o) => {
  var found = m.find(e => === o);
  found ? found.y++ : m.push({name: o, y: 1});
  return m;
}, []);


JavaScript Array reduce() Method, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  Array; Plain objects also support similar methods, but the syntax is a bit different. Object.keys, values, entries. For plain objects, the following methods are available: Object.keys(obj) – returns an array of keys. Object.values(obj) – returns an array of values. Object.entries(obj) – returns an array of [key, value] pairs.

ASP Dictionary Object, Dictionaries cannot be multidimensional, Arrays can; Dictionaries have more built​-in functions than Arrays; Dictionaries work better than arrays on accessing  The call to new Array(number) creates an array with the given length, but without elements. The length property is the array length or, to be precise, its last numeric index plus one. It is auto-adjusted by array methods. If we shorten length manually, the array is truncated. We can use an array as a deque with the following operations:

JavaScript Array filter() Method, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  array .length = number. Technical Details. A Number, representing the number of elements in the array object. JavaScript Version: JavaScript Tutorial: JavaScript Arrays. JavaScript Array Reference. If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:, Learning Center · IRC/Chat · Forums · Stack Overflow · Commercial Support The $.map() method applies a function to each item in an array or object and maps the results into The translation function that is provided to this method is called for each top-level <script src=""></​script>. Rewriting JavaScript: Converting an Array of Objects to an Object. Converting an Array of Objects to an If our data is organized in this format you can simply select a person based on

  • So what did you try?
  • Are you able to get rid of the array and just have an object such as var obj = {'A': 2, 'B': 4, 'C': 2}?
  • I would call your variable something other than a function name. myMap would work...
  • @Matt I was thinking the same. Done.