Object、Array是JS的引用型基本属性
枚举Object中的属性:
检查对象中是否有某个属性并赋值:
方法1
方法2
串联数组元素
[quote]The Array.join() method converts all the elements of an array to strings and concatenates them.[/quote]
可以指定用什么字符来串联:
也就是String.spilt()的逆向方法
数组反向
[quote]The Array.reverse() method reverses the order of the elements of an array and returns the reversed array.[/quote]
数组排序
[quote]Array.sort() sorts the elements of an array in place and returns the sorted array.[/quote]
默认通过以字母表进行排序。也可以提供排序的函数。
[quote]To sort an array into some order other than alphabetical, you must pass a comparison function as an argument to sort().
This function decides which of its two arguments should appear first in the sorted array.
If the first argument should appear before the second, the comparison function should return a number less than zero.
If the first argument should appear after the second in the sorted array, the function should return a number greater than zero.
And if the two values are equivalent (i.e., if their order is irrelevant), the comparison function should return 0.[/quote]
把判定函数当作参数传递给Array.sort(),该函数也有两个函数,例如a,b。
如果a在b的前面,则返回一个小于0的数;
如果a在b的后面,则返回一个大于0的数;
如果两则等效,则返回0。
数组的连接
[quote]The Array.concat() method creates and returns a new array that contains the elements of the original array on which concat() was invoked, followed by each of the arguments to concat(). [/quote]
数组的子集
[quote]The Array.slice() method returns a slice, or subarray, of the specified array. Its two arguments specify the start and end of the slice to be returned. The returned array contains the element specified by the first argument and all subsequent elements up to, but not including, the element specified by the second argument. [/quote]
Arrat.slice()接受两个参数,其返回值是原数组的子数组,由下标为第一个参数,一直到下标为(第二个参数-1)的元素组成
数组的切割
[quote]The Array.splice() method is a general-purpose method for inserting or removing elements from an array. splice() modifies the array in place; it does not return a new array, as slice() and concat() do.[/quote]
该方法会修改原数组,如果只提供一个参数,那么从下标值为参数的元素(包括这个元素)在原数组中开始删除,该方法返回删除的那些元素组成的数组
如果提供两个参数,那么第一个参数作用不变,第二个参数则限定删除元素的个数
如果提供两个以上的参数,那么从第三个参数开始就是执行了删除操作之后,将第三个参数之后的所有元素,插入到下标为第一个参数的元素的后方
用数组实现Stack
[quote]The push() and pop() methods allow you to work with arrays as if they were stacks.
[/quote]
用数组实现只能在前端进出的顺序表
[quote]The unshift() and shift() methods behave much like push() and pop(), except that they insert and remove elements from the beginning of an array rather than from the end.[/quote]
将数组转换为字符串
[quote]An array, like any JavaScript object, has a toString() method. For an array, this method converts each of its elements to a string (calling the toString() methods of its elements, if necessary) and outputs a comma-separated list of those strings. Note that the output does not include square brackets or any other sort of delimiter around the array value. [/quote]
这是一个对每个元素调用.toString()的过程,并将结果用逗号间隔返回
枚举Object中的属性:
function DisplayPropertyNames(obj) {
var names = "";
for(var name in obj) names += name + "\n";
alert(names);
}
检查对象中是否有某个属性并赋值:
方法1
// If o has a property named "x", then set it
if ("x" in o) o.x = 1;
方法2
// If the property x exists and is not undefined, set it.
// !== and === distinguish between undefined and null
if (o.x !== undefined) o.x = 1;
串联数组元素
[quote]The Array.join() method converts all the elements of an array to strings and concatenates them.[/quote]
var a = [1, 2, 3]; // Create a new array with these three elements
var s = a.join(); // s == "1,2,3"
可以指定用什么字符来串联:
s = a.join(", "); // s == "1, 2, 3"
也就是String.spilt()的逆向方法
数组反向
[quote]The Array.reverse() method reverses the order of the elements of an array and returns the reversed array.[/quote]
var a = new Array(1,2,3); // a[0] = 1, a[1] = 2, a[2] = 3
a.reverse(); // now a[0] = 3, a[1] = 2, a[2] = 1
var s = a.join(); // s == "3,2,1"
数组排序
[quote]Array.sort() sorts the elements of an array in place and returns the sorted array.[/quote]
默认通过以字母表进行排序。也可以提供排序的函数。
[quote]To sort an array into some order other than alphabetical, you must pass a comparison function as an argument to sort().
This function decides which of its two arguments should appear first in the sorted array.
If the first argument should appear before the second, the comparison function should return a number less than zero.
If the first argument should appear after the second in the sorted array, the function should return a number greater than zero.
And if the two values are equivalent (i.e., if their order is irrelevant), the comparison function should return 0.[/quote]
把判定函数当作参数传递给Array.sort(),该函数也有两个函数,例如a,b。
如果a在b的前面,则返回一个小于0的数;
如果a在b的后面,则返回一个大于0的数;
如果两则等效,则返回0。
var a = [33, 4, 1111, 222];
a.sort(); // Alphabetical order: 1111, 222, 33, 4
a.sort(function(a,b) { // Numerical order: 4, 33, 222, 1111
return a-b; // Returns < 0, 0, or > 0, depending on order
});
数组的连接
[quote]The Array.concat() method creates and returns a new array that contains the elements of the original array on which concat() was invoked, followed by each of the arguments to concat(). [/quote]
var a = [1,2,3];
a.concat(4, 5) // Returns [1,2,3,4,5]
a.concat([4,5]); // Returns [1,2,3,4,5]
a.concat([4,5],[6,7]) // Returns [1,2,3,4,5,6,7]
a.concat(4, [5,[6,7]]) // Returns [1,2,3,4,5,[6,7]]
数组的子集
[quote]The Array.slice() method returns a slice, or subarray, of the specified array. Its two arguments specify the start and end of the slice to be returned. The returned array contains the element specified by the first argument and all subsequent elements up to, but not including, the element specified by the second argument. [/quote]
Arrat.slice()接受两个参数,其返回值是原数组的子数组,由下标为第一个参数,一直到下标为(第二个参数-1)的元素组成
var a = [1,2,3,4,5];
a.slice(0,3); // Returns [1,2,3]
a.slice(3); // Returns [4,5]
a.slice(1,-1); // Returns [2,3,4]
a.slice(-3,-2); // Returns [3]
数组的切割
[quote]The Array.splice() method is a general-purpose method for inserting or removing elements from an array. splice() modifies the array in place; it does not return a new array, as slice() and concat() do.[/quote]
该方法会修改原数组,如果只提供一个参数,那么从下标值为参数的元素(包括这个元素)在原数组中开始删除,该方法返回删除的那些元素组成的数组
如果提供两个参数,那么第一个参数作用不变,第二个参数则限定删除元素的个数
var a = [1,2,3,4,5,6,7,8];
a.splice(4); // Returns [5,6,7,8]; a is [1,2,3,4]
a.splice(1,2); // Returns [2,3]; a is [1,4]
a.splice(1,1); // Returns [4]; a is [1]
如果提供两个以上的参数,那么从第三个参数开始就是执行了删除操作之后,将第三个参数之后的所有元素,插入到下标为第一个参数的元素的后方
var a = [1,2,3,4,5];
a.splice(2,0,'a','b'); // Returns []; a is [1,2,'a','b',3,4,5]
a.splice(2,2,[1,2],3); // Returns ['a','b']; a is [1,2,[1,2],3,3,4,5]
用数组实现Stack
[quote]The push() and pop() methods allow you to work with arrays as if they were stacks.
[/quote]
var stack = []; // stack: []
stack.push(1,2); // stack: [1,2] Returns 2
stack.pop(); // stack: [1] Returns 2
stack.push(3); // stack: [1,3] Returns 2
stack.pop(); // stack: [1] Returns 3
stack.push([4,5]); // stack: [1,[4,5]] Returns 2
stack.pop() // stack: [1] Returns [4,5]
stack.pop(); // stack: [] Returns 1
用数组实现只能在前端进出的顺序表
[quote]The unshift() and shift() methods behave much like push() and pop(), except that they insert and remove elements from the beginning of an array rather than from the end.[/quote]
var a = []; // a:[]
a.unshift(1); // a:[1] Returns: 1
a.unshift(22); // a:[22,1] Returns: 2
a.shift(); // a:[1] Returns: 22
a.unshift(3,[4,5]); // a:[3,[4,5],1] Returns: 3
a.shift(); // a:[[4,5],1] Returns: 3
a.shift(); // a:[1] Returns: [4,5]
a.shift(); // a:[] Returns: 1
将数组转换为字符串
[quote]An array, like any JavaScript object, has a toString() method. For an array, this method converts each of its elements to a string (calling the toString() methods of its elements, if necessary) and outputs a comma-separated list of those strings. Note that the output does not include square brackets or any other sort of delimiter around the array value. [/quote]
这是一个对每个元素调用.toString()的过程,并将结果用逗号间隔返回
[1,2,3].toString() // Yields '1,2,3'
["a", "b", "c"].toString() // Yields 'a,b,c'
[1, [2,'c']].toString() // Yields '1,2,c'