MarcL
MarcL

Reputation: 3593

Javascript add 2 new keys dynamically to new dictionary object or increment if they exist

I am trying to create a dictionary in javascript based on two data key-value pairs (I want to do a kind of a grouping approach with it) my data has 2 fields: name, (either "reserved" or "storned") and a month name field: somedata = {name: "July", value:"reserved"} now, I want to add up the corresponding months into a bucket, and increment by +1 for each group they correspond to (either reserved or storned). How can I dynamically iterate over the objects in an array an create dynamically a dictionary object with key as months, and then value (or other field as reserved) and increment +1 each time the same object of data comes in? So this is basically a group by month and value object...

in Python thats quite simple:

ist_of_urls = ['http://www.google.fr/', 'http://www.google.fr/', 
                'http://www.google.cn/', 'http://www.google.com/', 
                'http://www.google.fr/', 'http://www.google.fr/', 
                'http://www.google.fr/', 'http://www.google.com/', 
                'http://www.google.fr/', 'http://www.google.com/', 
                'http://www.google.cn/']

urls = [{'url': 'http://www.google.fr/', 'nbr': 1}]

    urls_d = {}
    for url in list_of_urls:
        if not url in urls_d:
            urls_d[url] = 1
        else:
            urls_d[url] += 1

for one key url, but I don't know how to do it for 2 keys in javascript...

EDIT: to make it more precise, my task was to generate a certain data format for ngx charts:

{name: "...", series: [{name:"..",value:".."},{...}]}

Some raw data coming from an api:

[
{
"Anzahl": 1,
"DemoID": 123456789,
"Monat": "August"
},
{
"Anzahl": 2,
"DemoID": 123456789,
"Monat": "April"
},
{
"Anzahl": 2,
"DemoID": 123456789,
"Monat": "Mai"
}
]

I previously made an array of 12 months:

docArray= [
    {name: "Januar", series: []}, {name: "Februar", series: []}, {name: "Maerz", series: []}, {name: "April", series: []},
    {name: "Mai", series: []}, {name: "Juni", series: []}, {name: "Juli", series: []}, {name: "August", series: []},
    {name: "September", series: []}, {name: "Oktober", series: []}, {name: "November", series: []}, {name: "Dezember", series:[]}
  ]

For a stacked bar chart, I have to prep the data so it fits the data schema and put it all in one array, ("docArray"): For the two kinds of data, I used two different formatting methods (inside angular subscribe method ;) )

formatStackedBarChartReservierungen(data: any[]){
    for (var dok of data) {
      for (var j = 0; j< this.docArray.length; j++) {
        if (this.docArray[j].name == dok.name){
          var newDok = {name: "Reservierungen", value: dok.value}   ;
          this.docArray[j].series.push(newDok);
        }
      }
    }

    return this.docArray;
  }

  formatStackedBarChartStornierungen(data:any[]){
    for (var dok of data) {
      for (var j = 0; j< this.docArray.length; j++) {
        if (this.docArray[j].name == dok.name){
          var newDok = {name: "Stornierungen", value: dok.value}   ;
          this.docArray[j].series.push(newDok);
        }
      }
    }    
    return this.docArray;
  }

Finally, I would like to get something like this:

exampleTestData= [{name: "Januar", series: [{name: "Reserviert", value: 2}, {name: "Storniert", value: 8}]},
  {name: "Mai", series: [{name: "Reserviert", value: 5}, {name: "Storniert", value: 3}]} ]    

This approach just looks just very tedious to me, what if there were 100 or 1000 different values in the array or unknowns...?

Upvotes: 0

Views: 145

Answers (2)

jh314
jh314

Reputation: 27802

Here is the JS analog for your Python example:

 const urls_d = {};
 for (const url of list_of_urls) {
   if (urls_d[url]) {
     urls_d[url]++;
   } else {
     urls_d[url] = 1;
   }
 }

Upvotes: 2

Nina Scholz
Nina Scholz

Reputation: 386512

For counting, you could take an object and use a default value if the url is not found in the object.

var array = ['http://www.google.fr/', 'http://www.google.fr/', 'http://www.google.cn/', 'http://www.google.com/', 'http://www.google.fr/', 'http://www.google.fr/', 'http://www.google.fr/', 'http://www.google.com/', 'http://www.google.fr/', 'http://www.google.com/', 'http://www.google.cn/'],
    count = array.reduce((c, s) => (c[s] = (c[s] || 0) + 1, c), Object.create(null));
    
console.log(count);

Upvotes: 0

Related Questions