1

Below I have a set of data I receive from a server.

I need to find from the data which "id" is connected to which "apip"

I have working code below.

My question is...did I do it in the most efficient way possible ?

My strategy was as follows:

Step 1 - Capture All the apip save them into an array

Step 2 - clear out all repeating apip

Step 3 - for loop to compare if apip matches apip

-If true capture that ID

-else store that ID on apip 2

(IT IS KNOW that I only have 2 possible APIPs)

Step 4 - clear out any repeating "ids"

Was this the best approach ?

var data = [{
  "time": "1571965891.8420029",
  "rssi": "30",
  "id": "123456789",
  "apip": "172.172.172.172.1"
}, {
  "time": "1571971066.8283374",
  "rssi": "30",
  "id": "100",
  "apip": "172.172.172.172.2"
}, {
  "time": "1571965476.4821894",
  "rssi": "30",
  "id": "123456789",
  "apip": "172.172.172.172.2"
}, {
  "time": "1571965894.140705",
  "rssi": "30",
  "id": "123456789",
  "apip": "172.172.172.172.2"
}, {
  "time": "1571965893.1654441",
  "rssi": "30",
  "id": "123456789",
  "apip": "172.172.172.172.1"
}, {
  "time": "1571970952.7499905",
  "rssi": "30",
  "id": "9999999",
  "apip": "172.172.172.172.1"
}, {
  "time": "1571965888.1338017",
  "rssi": "30",
  "id": "123456789",
  "apip": "172.172.172.172.2"
}, {
  "time": "1571970925.342063",
  "rssi": "30",
  "id": "66666",
  "apip": "172.172.172.172.1"
}, {
  "time": "1571965890.158157",
  "rssi": "30",
  "id": "123456789",
  "apip": "172.172.172.172.1"
}]

console.log(data)

var APs = [];
var dataFromAP1 = [];
var dataFromAP2 = [];
data.forEach(item => {
  APs.push(item.apip);
});
console.log(APs);
var uniq = [...new Set(APs)];
console.log(uniq);

data.forEach(item => {
  if (item.apip === uniq[0]) {
    dataFromAP1.push(item.id);
  } else {
    dataFromAP2.push(item.id);
  }
});

var uniqdataFromAP1 = [...new Set(dataFromAP1)];
console.log(uniqdataFromAP1);

var uniqdataFromAP2 = [...new Set(dataFromAP2)];
console.log(uniqdataFromAP2);

2 Answers 2

2

To find the one of the uniqs (let's call it oneApip), just check the first element of the data - you dont need a Set or to iterate over all of them. Then iterate over the data and, for each element, add to one set or the other, depending on whether its apip matches that oneApip:

var data=[{time:"1571965891.8420029",rssi:"30",id:"123456789",apip:"172.172.172.172.1"},{time:"1571971066.8283374",rssi:"30",id:"100",apip:"172.172.172.172.2"},{time:"1571965476.4821894",rssi:"30",id:"123456789",apip:"172.172.172.172.2"},{time:"1571965894.140705",rssi:"30",id:"123456789",apip:"172.172.172.172.2"},{time:"1571965893.1654441",rssi:"30",id:"123456789",apip:"172.172.172.172.1"},{time:"1571970952.7499905",rssi:"30",id:"9999999",apip:"172.172.172.172.1"},{time:"1571965888.1338017",rssi:"30",id:"123456789",apip:"172.172.172.172.2"},{time:"1571970925.342063",rssi:"30",id:"66666",apip:"172.172.172.172.1"},{time:"1571965890.158157",rssi:"30",id:"123456789",apip:"172.172.172.172.1"}];

const oneApip = data[0].apip;
const set1 = new Set();
const set2 = new Set();
for (const { apip, id } of data) {
  (apip === oneApip ? set1 : set2).add(id);
}
const uniq1 = [...set1];
const uniq2 = [...set2];
console.log(uniq1);
console.log(uniq2);

More generally, for any number of apips, create an object indexed by apip, whose values are sets:

var data=[{time:"1571965891.8420029",rssi:"30",id:"123456789",apip:"172.172.172.172.1"},{time:"1571971066.8283374",rssi:"30",id:"100",apip:"172.172.172.172.2"},{time:"1571965476.4821894",rssi:"30",id:"123456789",apip:"172.172.172.172.2"},{time:"1571965894.140705",rssi:"30",id:"123456789",apip:"172.172.172.172.2"},{time:"1571965893.1654441",rssi:"30",id:"123456789",apip:"172.172.172.172.1"},{time:"1571970952.7499905",rssi:"30",id:"9999999",apip:"172.172.172.172.1"},{time:"1571965888.1338017",rssi:"30",id:"123456789",apip:"172.172.172.172.2"},{time:"1571970925.342063",rssi:"30",id:"66666",apip:"172.172.172.172.1"},{time:"1571965890.158157",rssi:"30",id:"123456789",apip:"172.172.172.172.1"}];

const grouped = {};
for (const { apip, id } of data) {
  if (!grouped[apip]) {
    grouped[apip] = new Set();
  }
  grouped[apip].add(id);
}
const arrs = Object.entries(grouped)
  .map(([apip, set]) => [apip, [...set]]);
console.log(arrs);

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

1 Comment

wow that's some nice clean code right there, thank you thank you
1

You can do:

const data = [{"time": "1571965891.8420029","rssi": "30","id": "123456789","apip": "172.172.172.172.1"}, {"time": "1571971066.8283374","rssi": "30","id": "100","apip": "172.172.172.172.2"}, {"time": "1571965476.4821894","rssi": "30","id": "123456789","apip": "172.172.172.172.2"}, {"time": "1571965894.140705","rssi": "30","id": "123456789","apip": "172.172.172.172.2"}, {"time": "1571965893.1654441","rssi": "30","id": "123456789","apip": "172.172.172.172.1"}, {"time": "1571970952.7499905","rssi": "30","id": "9999999","apip": "172.172.172.172.1"}, {"time": "1571965888.1338017","rssi": "30","id": "123456789","apip": "172.172.172.172.2"}, {"time": "1571970925.342063","rssi": "30","id": "66666","apip": "172.172.172.172.1"}, {"time": "1571965890.158157","rssi": "30","id": "123456789","apip": "172.172.172.172.1"}]

const sets = data.reduce(
    (a, { apip, id }, _, arr) => ((apip === arr[0].apip ? a.one : a.two).add(id), a),
    { 
      one: new Set(), 
      two: new Set() 
    }
  ),
  result = {
    uniqDataFromAP1: [...sets.one],
    uniqDataFromAP2: [...sets.two]
  }

console.log(result)
.as-console-wrapper {max-height: 100% !important;top: 0;}

Comments

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.