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