0

I am new in Javascript. I have a JS method. I got so many lists of wifi network. But almost time SSID and SecurityMode is coming as same. I want to filter if both are same. Like suppose SSID is "sampleWifi" & SecurityMode is "PSK" and same SSID and SecurityMode are coming again. How to avoid that to add a list array.

My code is like below :

device.networkList = [];
  for (var i = 1; i <= jqXHR.responseJSON.length; i++) {
    site = jqXHR.responseJSON[i - 1];
    device.networkList.push({
      name: site["AvailableNetworks." + i + ".SSID"],
      signal: site["AvailableNetworks." + i + ".SignalStrength"],
      security: site["AvailableNetworks." + i + ".SecurityMode"]
    });
  }

Output is : A PSK, B PSK, C PSK, D PSK, A PSK, B PSK, R PSK, S PSK

Expected Output : A PSK, B PSK, C PSK, D PSK, R PSK, S PSK

Note : Here same A PSK, B PSK is repeated in array. But i don't want to add same duplicate value in array.

How to put a condition is SecurityMode will come it shouldn't be add device.networkList array.

Please help me.

Thanks

Soumya Ranjan
  • 4,817
  • 2
  • 26
  • 51

4 Answers4

1

as Rayon stated, you just need to go through the array and check if similar values were previously given.

It could be a function like this:

function isDuplicate(networks, name, security) {
    for (var i = 0; i < networks.length; i++) {
        if(networks[i].name == name && networks[i].security == security) // Found duplicate
            return true;
    }

    return false; // Doesn't already exist
}
Abdelhakeem Osama
  • 143
  • 1
  • 1
  • 9
1

You can use an object or a Set (if ES6 capable) to keep track of what you've already added and then check that Set in a conditional. You can create a key for the set with a combination of the ssid and the securityMode so you can account for both items in one key;

device.networkList = [];
var soFar = new Set();
jqXHR.responseJSON.forEach(function(site, index) {
    var key = site.SSID + site.securityMode;
    var i = index + 1;
    if (!soFar.has(key)) {
        device.networkList.push({
            name: site["AvailableNetworks." + i + ".SSID"],
            signal: site["AvailableNetworks." + i + ".SignalStrength"],
            security: site["AvailableNetworks." + i + ".SecurityMode"]
        })
        soFar.add(key);
    }
});

See Mimicking sets in JavaScript? for a Set polyfill.

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • site.SSID & site.securityMode is coming undefined – Soumya Ranjan Sep 29 '16 at 06:03
  • @SRNayak - I'm done here! If you would just SHOW us your actual JSON, then we could stop guessing what fields are in it. You should be able to adapt this code to whatever the actual property names are in your mystery JSON that you refuse to show us. This shows you how to do it. We can't read your mind on property names you don't show. Take this code and adapt it to match your actual JSON. I've asked you three times to show us your actual JSON and you refuse. I'm done asking you again and again and again. – jfriend00 Sep 29 '16 at 06:05
  • I understand ur word. but problem is whatever i posted the code above , i want to in that code only. No need to change anything only have to give a condition. coz that code I can't change without permission. – Soumya Ranjan Sep 29 '16 at 06:09
  • @SRNayak - I showed you how to do it. If you don't want to adapt that to the actual property names in your JSON which you refused to share with us, then that's not my problem. I am done responding to you unless you disclose the entire problem. – jfriend00 Sep 29 '16 at 06:12
  • Thanks for ur answer @ jfriend00 :) – Soumya Ranjan Sep 29 '16 at 09:16
1

Yes I got it.

 var sap_ssid ;
  var sap_signalStrength;
  var sap_securityMode;

  device.networkList = [];
  for (var i = 1; i <= jqXHR.responseJSON.length; i++) {
      site = jqXHR.responseJSON[i - 1];

      sap_ssid = site["AvailableNetworks." + i + ".SSID"];
      sap_signalStrength = site["AvailableNetworks." + i + ".SignalStrength"];
      sap_securityMode = site["AvailableNetworks." + i + ".SecurityMode"];

      if (device.networkList.length == 0)
      {
          device.networkList.push({
                                  name: sap_ssid,
                                  signal: sap_signalStrength,
                                  security: sap_securityMode
                                  });
      }
      else{
          var isfind = false;
          for(var j = 0; j < device.networkList.length; j++)
          {
              if (device.networkList[j].name == sap_ssid && device.networkList[j].security == sap_securityMode)
              {
                  isfind = true;
                  break;
              }
          }


          if (!isfind)
          {
              device.networkList.push({
                                      name: sap_ssid,
                                      signal: sap_signalStrength,
                                      security: sap_securityMode
                                      });
          }
      }
  }
Soumya Ranjan
  • 4,817
  • 2
  • 26
  • 51
0

You need to have a function or code for read all values in your array and make a validation via SSID and SecurityMode, there isn't a native function for check exists in array for user defined objects.

Generic function:

function exists(array, func) {
    var value = false;

    for (var i = 0; i < array.length; i++) {
        value = func(array[i]);

        if (value == true) {
            break;
        }
    }

    return value;
};

Data:

var array = [];

array.push({ name: "foo", signal: "123", security: "integrated" });
array.push({ name: "bar", signal: "456", security: "none" });
array.push({ name: "zaz", signal: "984", security: "ntm" });

Usage:

var foo = exists(array, function (item) {
    return item.name == "foo" && item.security == "integrated";
});

console.log(foo); // true
H. Herzl
  • 3,030
  • 1
  • 14
  • 19