Array string correlation in JS

Source: Internet
Author: User
Tags array to string hasownproperty

1. String to Array

var s = "abc,abcd,aaa";
SS = S.split (",");//decomposition at each comma (,).

2. Array to String

var a, B;
A = new Array (0,1,2,3,4);
b = A.join (",");

3. Query whether an array contains an element

var Num=jquery.inarray (value, array);

Explanation: Returns the position of value in the array, starting with a count of 0 (1 if not found).

Remove Duplicates in 4.js

Refer to other people to summarize:

4.1

Array.prototype.unique1 = function() {

     var r = new Array();      label: for ( var i = 0, n = this .length; i < n; i++) {          for ( var x = 0, y = r.length; x < y; x++) {              if (r[x] == this [i]) {                  continue label;              }          }          r[r.length] = this [i];      }      return r; }4.2 Regular Array.prototype.unique2 = function () {      return this .sort().join( ",," ).replace(/(,|^)([^,]+)(,,\2)+(,|$)/g, "$1$2$4" ).replace(/,,+/g, "," ).replace(/,$/, "" ).split( "," ); }4.3 Using the object's "hasOwnProperty" method rray.prototype.unique3 = function () {      var temp = {}, len = this .length;      for ( var i=0; i < len; i++)  {          var tmp = this [i];          if (!temp.hasOwnProperty(tmp)) {              temp[ this [i]] = "my god" ;          }      }        len = 0;      var tempArr=[];      for ( var i in temp) {          tempArr[len++] = i;      }      return tempArr; }4.4 First Order, the preceding paragraph than latter. This method is quite simple, but also practical. Array.prototype.unique4 = function () {      var temp = new Array();        this .sort();        for (i = 0; i < this .length; i++) {            if ( this [i] == this [i+1]) {              continue ;          }            temp[temp.length]= this [i];        }        return temp;   }4.5 High efficiency Array.prototype.unique5 = function () {      var res = [], hash = {};      for ( var i=0, elem; (elem = this [i]) != null ; i++)  {          if (!hash[elem])          {              res.push(elem);              hash[elem] = true ;          }      }      return res; }

Array string correlation in JS

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.