JS Dynamic Add unknown new property

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.