0

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"

   ...
3
  • 1
    Please read the docs for every, that's not what it's for. You probably want to use map Commented Dec 6, 2020 at 23:15
  • 3
    you would be better of provindg the rows structure and the output you want Commented Dec 6, 2020 at 23:17
  • 1
    Actually this is a specific function for datatables.net - it looks through the its own data. There is no issue at all with data values being correct - the issue is creating grouped how I want to. Commented Dec 6, 2020 at 23:17

2 Answers 2

1

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);
  }

});
Sign up to request clarification or add additional context in comments.

1 Comment

Yes. This is more or less what I was originally trying to do. Thank you.
1

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

In the original data there can be duplicates so 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.
'rows() is not iterable'. I need to loop through the original data with dt.rows().every(function() { var data = this.data(); .... like I have in the post. It's the only way to loop through its data.
@user756659 ok, it has total now and uses "every" with that strange "this.data" construct.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.