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