Been awhile since I had to do something like this in js and having some trouble with it.
Basically I am looping looping through an array that contains ip_address, city, location, postal, and country. A given ip_address will always have the same city, location, postal, and country values. In my loop I want to create a new array which removes the duplicates and creates a 'total' value that keeps track of how many times that ip_address was in the results.
I have a feeling I've been working with php too much as this isn't the same, but having trouble getting what I want. I'd like the results of grouped to be the ip_address values and then be able to call grouped['ip_address']['city'] and so on to get that ip's other values.
//loop through dt results and create an array of grouped ip addresses
var grouped = [];
dt.rows().every(function() {
var data = this.data();
grouped['ip_address'] = data['ip_address'];
grouped['ip_address']['city'] = data['city'];
grouped['ip_address']['location'] = data['location'];
grouped['ip_address']['postal'] = data['postal'];
grouped['ip_address']['country'] = data['country'];
grouped['ip_address']['total'] = grouped['ip_address']['total'] ? grouped['ip_address']['total'] + 1 : 1;
});
console.log(grouped);
example data :
ip_address "111.111.111.111"
city "Miami"
location "Florida"
postal "12458"
country "USA"
ip_address "222.222.222.222"
city "Orlando"
location "Florida"
postal "12423"
country "USA"
ip_address "111.111.111.111"
city "Miami"
location "Florida"
postal "12458"
country "USA"
...
result I would like :
ip_address "111.111.111.111"
city "Miami"
location "Florida"
postal "12458"
country "USA"
total "2"
ip_address "222.222.222.222"
city "Orlando"
location "Florida"
postal "12423"
country "USA"
total "1"
...
every, that's not what it's for. You probably want to usemapdatavalues being correct - the issue is creatinggroupedhow I want to.