How to count and transform an array with a specific javascript condition

I have an array as shown below.

var inputData = [
    {label:1, time: Mon},
    {label:1, time: Tue},
    {label:2, time: Wed},
    {label:2, time: Mon},
    {label:2, time: Thu},
    {label:3, time: Tue},
    {label:3, time: Mon},
    {label:3, time: Mon},
    {label:3, time: Thu},
    {label:1, time: Fri},
    ......
];

Label 1 to 3, time from Monday to Friday. I want to count the amount of each combination of shortcuts and time. Finally, I need an array like this:

var newArray = [
    {label:1, time:Mon, count:19},
    {label:1, time:Tue, count:34},
    {label:1, time:Wed, count:36},
    {label:1, time:Thu, count:21},
    {label:1, time:Fri, count:32},
    {label:2, time:Mon, count:55},
    {label:2, time:Tue, count:25},
    {label:2, time:Wed, count:87},
    {label:2, time:Thu, count:44},
    {label:2, time:Fri, count:56},
    ......
];

This is what I tried, but unfortunately it did not work. Can anyone help? Appreciate!

var labelArr = d3.map(inputData, function(d) {return d['label'];}).keys();
        //labelArr = ["1","2","3"];
var filteredTime = d3.map(inputData, function(d) {return d['time'];}).keys();   
    //filteredTime = ["Mon", "Tue","Wed","Thu","Fri"];

        var newArray = [];
        for(var i = 0;i < filteredTime.length; i++){
            var count = 0;
            for(var j = 0; j < inputData.length; j++){
            if(inputData[j]['time'] === filteredTime[i]){
                for(var s = 0; s < labelArr.length; s++){                   
                    if(inputData[j]['label'] === labelArr[s]){
                        count++;

                    }
                }
            }
        }   
        newArray.push({
            'label':labelArr[s],
            'time': filteredTime[i],
            'count':count
        })
    }
+4
source share
2 answers

Here is your code slightly redone: -

var labelArr = d3.map(inputData, function(d) {
  return d['label'];
}).keys();

var filteredTime = d3.map(inputData, function(d) {
  return d['time'];
}).keys();

var newArray = [];
for (var i = 0; i < filteredTime.length; i++) {
  for (var s = 0; s < labelArr.length; s++) {
    var count = 0;
    for (var j = 0; j < inputData.length; j++) {
      if (inputData[j]['time'] == filteredTime[i] && inputData[j]['label'] == labelArr[s])
        count++;
    }
    newArray.push({
      'label': labelArr[s],
      'time': filteredTime[i],
      'count': count
    });
  }
}

Your main problem came down to .keysreturning an array of strings for the label. one ===that also compares the type returned false ( int === string).

+2
source

. label time , .

var map = {};

inputData.forEach(function(d) {
  var key = "" + d.label + d.time;  
  if (!map[key]) {       // if the key is not yet in the map...
    d.count = 0;         // add a count property to the object
    map[key] = d;        // and add the object to the map
  }
  map[key].count++       // increase object counter
});

, d3.values():

var newArray = d3.values(map);

:

"use strict";

var inputData = [{
  label: 1,
  time: "Mon"
}, {
  label: 1,
  time: "Tue"
}, {
  label: 2,
  time: "Wed"
}, {
  label: 2,
  time: "Mon"
}, {
  label: 2,
  time: "Thu"
}, {
  label: 3,
  time: "Tue"
}, {
  label: 3,
  time: "Mon"
}, {
  label: 3,
  time: "Mon"
}, {
  label: 3,
  time: "Thu"
}, {
  label: 1,
  time: "Fri"
}, ];

var map = {};
inputData.forEach(function(d) {
  var key = "" + d.label + d.time;  
  if (!map[key]) {       // if the key is not yet in the map...
    d.count = 0;         // add a count property to the object
    map[key] = d;        // and add the object to the map
  }
  map[key].count++       // increase object counter
});

var newArray = d3.values(map);  // we are only interested in the array of values

//////// Just beautifying output below...
d3.select("p").append("text")
	.text(JSON.stringify(newArray, null, " "));
p {
  white-space: pre;
  font-family: monospace;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

<p></p>
Hide result
+1

Source: https://habr.com/ru/post/1628397/


All Articles