1 Preface
As a result of the project needs, need to achieve a different network card to get the server to sweep the LAN of some type of device, however, the number of different server network cards are not the same, so you need to dynamically add unknown properties to count the number of devices scanned by different network cards.
2 Code
function Nicclassify (JSON) {var ret = {};var Jsonarr = Json.split (","); var Setarr = Uniquearr (Jsonarr); var map={};if (json = = "") {return ret;} initret.wireless = 0;var K=0;for (var j = 0; J < setarr.length;j++) {var data = Setarr[j].tolowercase (); Data.indexof ("wireless") >-1 | | Data.indexof ("WiFi") >-1 | | Data.indexof ("WLAN") >-1 | | Data.indexof ("Wi-Fi") >-1) {eval ("Ret.wire" +k+ "=0"), eval ("map[" "+r (data) +" ']= ' Wire "+k+" "); k++;}} for (var i = 0; i < jsonarr.length; i++) {var data = Jsonarr[i].tolowercase (); if (Data.indexof ("wireless") >-1 | | | data . indexOf ("WiFi") >-1 | | Data.indexof ("WLAN") >-1 | | Data.indexof ("Wi-Fi") >-1) {ret.wireless + +;} else {for (var kk = 0; kk < K; kk++) {if (eval ("map['" +r (data) + ']= ' wire "+kk+" ')) {eval ("Ret.wire" +kk+ "+ +");}}} return ret;} function R (s) {return S.replaceall ("", "_");} function Uniquearr (array) {var n = [];for (var i = 0; i < Array.Length; i++) {if (N.indexof (array[i]) = =-1) n.push (array [i]); } return N; }Sample:jsonarrnics = "Wifi,wi-fi,a,a,b,b,b,wlan"; var nicretmap = nicclassify (jsonarrnics);
3 description
In general, MAP.ATTR1 = Val1 is equivalent to map["attr1"] = val1, but when ATTR1 is marked with ', '-' and so on, it cannot be used directly '. ' operator to access.
JS Dynamic Add unknown new property