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"
...
2 Answers 2
This should be easy
//loop through dt results and create an array of grouped ip addresses
var grouped = [];
dt.rows().every(function() {
var data = this.data();
var item = {};
// If the item with the same ipaddress already exist in (grouped) then append total, else push new item to (grouped).
var foundItem = grouped.find((a) => a.ip_address == data.ip_address);
if (foundItem)
foundItem.total += 1;
else {
item.ip_address = data['ip_address'];
item.city = data['city'];
item.location = data['location'];
item.postal = data['postal'];
item.country = data['country'];
item.total = 1;
grouped.push(item);
}
});
1 Comment
You seem to want something like
var grouped=new Map();
dt.rows().every(function(){
let data=this.data;
let ip=data.ip_address;
if(grouped.has(ip))
grouped.get(ip).total++;
else
grouped.set(data.ip_address,{
ip_address:data.ip_address,
city:data.city,
...
country:data.country,
total:0
});
}
but it's not sure, just a guess, and I can't decode the role of total from the question.
4 Comments
total just represents how many times the ip_address was found in the original data. The rest of its values (city, location, etc) will always be the same given the same ip_address.dt.rows().every(function() { var data = this.data(); .... like I have in the post. It's the only way to loop through its data.let data=this.data(); and total:1 had to be added/changed. This will work for me. I can do a grouped.forEach and run through all them like I wanted to. The 'strange' way is just how this particular plugin was created. It's the only way to run through its data in a loop.
every, that's not what it's for. You probably want to usemapdatavalues being correct - the issue is creatinggroupedhow I want to.