Array
- definition of array
literal;
var new new ARR = the Array (length / Content)
- Read and write the array
can not read overflow, returns undefined
but may overflow write, the middle is empty, (after this operation should be an array of class! - need to verify personal views!!)
ECMAScript is the DOM BOM js three parts!
Different browsers have differences in the DOM and BOM! In particular BOM! Operation of the browser BOM!
ES3.0 es5.0 es6.0
- An array of several methods commonly used!
- change the original array
7 Ways: the Push, POP, unshift, the Shift, splice, Reverse, the Sort - Source array does not change
- the concat, the Join <-> Split (STR), toString, Slice
- push: an array final add
Returns the length of the array!
push method of rewriting:
var arr = [1,2,3];
Array.prototype.push = function(){
for(var i = 0 ;i < arguments.length;i++){
this[this.length] = arguments[i];
}
return this.length;
}
- pop: Cut the last array, mass participation with no eggs
Insert pictures described herein
value or length of the returned array!
pop method overrides:
var arr = [1, 2, 3];
Array.prototype.pop = function() {
var popValue = this[this.length - 1];
this.length--;
return popValue;
};
- shift unshift: Save and processing at the beginning of an array
unshift add the required parameters! shift delete unnecessary parameters!
unshift method overrides:
var arr = [1, 2, 3];
Array.prototype.unshift = function() {
var len = arguments.length,
arg = [];
for (var j = 0; j < len; j++) {
arg[j] = arguments[j];
}
for (var i = 0; i < this.length; i++) {
arg[arg.len + i] = arg.push(this[i]);
}
return arg;
};
This approach is not good! To return the length of the words, arr has not changed!
- reverse: reverse order
Method overrides:
var arr = [1, 2, 3];
Array.prototype.reverse = function() {
var len = this.length,
revArr = [];
for (var i = 0; i < len; i++) {
revArr[i] = this[len - i - 1];
}
arr = revArr;
return arr;
};
- splice: Slice (beginning from the first of several, length taken, adding new data incision)
Adding a certain position, may be used to override the unshift
parameter start bit is negative
- sort: ascending sort code asc
So the system left us with an interface, let us write your own sorting rules
var arr = [1, 2, 6, 5, 20, 11];
//必须有两个参数
//看返回值,若返回值为负,那么前面的数放到前面
// 若返回值为正,那么后面的数放到前面
arr.sort(function(a, b) {
return a - b;
// return b-a;降序
});
To an ordered array of out of order (ha ha!)
var arr = [1, 2, 5, 6, 11, 20];
//必须有两个参数
//看返回值,若返回值为负,那么前面的数放到前面
// 若返回值为正,那么后面的数放到前面
arr.sort(function(a, b) {
return Math.random() - 0.5;
// return b-a;降序
});
Each refresh is different!
Sort by some value target object
var obj = {
age: 185
};
obj1 = {
age: 195
};
obj2 = {
age: 20
};
arr = [obj, obj1, obj2];
//必须有两个参数
//看返回值,若返回值为负,那么前面的数放到前面
// 若返回值为正,那么后面的数放到前面
arr.sort(function(a, b) {
return a.age - b.age;
// return b-a;降序
});
Byte ordering:
function retBytes(str) {
var num = str.length;
for (var i = 0; i < str.length; i++) {
if (str.charCodeAt(i) > 255) {
num++;
}
}
}
var arr = ["sfjslj", "a", "b", "c", "wangjia"];
arr.sort(function(a, b) {
return retBytes(a) - retBytes(b);
});
- concat: connecting two arrays (not alter the original array)
-slice :( beginning from the first of several interception, interception to this bit)
Note the difference between he and splice in!
-toString (): It do not say it!
Class array (an array of arrays can be used when the non-)
an array of arguments is a class
attribute to the index (digital) property, you must have a length, plus the best push,
Plus after the splice method becomes an array
Ali original title:
var obj = {
"2": "a",
"3": "b",
length: 2,
push: Array.prototype.push
// splice: Array.prototype.splice
};
obj.push("c");
obj.push("d");
var obj = {
"0": "s",
"1": "ss",
"2": "a",
"3": "b",
length: 4,
name: wangjia,
age: 23,
push: Array.prototype.push,
splice: Array.prototype.splice
};