js中Array和Object学习
来源:互联网 发布:山东邮政网络培训学院 编辑:程序博客网 时间:2024/05/19 14:34
Object
1、hasOwnProperty 方法
确定某个对象是否具有带指定名称的属性
object.hasOwnProperty(proName)
var s = new String("Sample");document.write(s.hasOwnProperty("split"));document.write("<br/>");document.write(String.prototype.hasOwnProperty("split"));// Output:// false// truevar arr=[1,2];arr.hasOwnProperty(1); //true
2、isPrototypeOf 方法
确定一个对象是否存在于另一个对象的原型链中。
prototype.isPrototypeOf(object)
function Rectangle() {}var rec = new Rectangle();document.write(Rectangle.prototype.isPrototypeOf(rec));// Output: true
Array
1、concat 方法
组合两个或两个以上的数组。
array1.concat([item1[, item2[, … [, itemN]]]])
var a, b, c, d;a = new Array(1,2,3);b = "dog";c = new Array(42, "cat");d = a.concat(b, c);document.write(d);//Output: 1, 2, 3, "dog", 42, "cat"
2、entries 方法
返回一个迭代器,它返回数组的键/值对。
arrayObj.entries();
var entries = ["a", "b", "c"].entries();// entries.next().value == [0, "a"]// entries.next().value == [1, "b"]// entries.next().value == [2, "c"]
3、every 方法
确定数组的所有成员是否满足指定的测试。
array1.every(callbackfn[, thisArg])
// Create a function that returns true if the value is// numeric and within range.var checkNumericRange = function(value) { if (typeof value !== 'number') return false; else return value >= this.minimum && value <= this.maximum;}// Create an array of numbers.var numbers = [10, 15, 19];// Check whether the callback function returns true for// all of the array values.// The obj argument enables use of the this value// within the callback function.var obj = { minimum: 10, maximum: 20 }if (numbers.every(checkNumericRange, obj)) document.write ("All are within range.");else document.write ("Some are not within range.");// Output:// All are within range.
4、fill 方法
使用指定值填充数组。
arrayObj.fill(value [ , start [ , end ] ]);
备注:如果 start 为负,则 start 被视为 length+start,即-1为最后一位,其中,length 是数组的长度。如果 end 为负,则 end 被视为 length+end。
[0, 0, 0].fill(7, 1);// Array contains [0,7,7][0, 0, 0].fill(7);// Array contains [7,7,7]
5、filter 方法
返回数组中的满足回调函数中指定的条件的元素。
array1.filter(callbackfn[, thisArg])
回调函数
function callbackfn(value, index, array1)
var checkNumericRange = function(value) { if (typeof value !== 'number') return false; else return value >= this.minimum && value <= this.maximum;}var numbers = [6, 12, "15", 16, "the", -12];// The obj argument enables use of the this value// within the callback function.var obj = { minimum: 10, maximum: 20 }var result = numbers.filter(checkNumericRange, obj);document.write(result);// Output: 12,16
6、findIndex 方法
返回满足回调函数中指定的测试条件的第一个数组元素的索引值。
arrayObj.findIndex(callbackfn [, thisArg]);
备注:对于数组中的每个元素,findIndex 方法都会调用一次回调函数(采用升序索引顺序),直到有元素返回 true。只要有一个元素返回 true,findIndex 立即返回该返回 true 的元素的索引值。如果数组中没有任何元素返回 true,则 findIndex 返回 -1。
回调函数
function callbackfn(value, index, thisArg)
[1,2,3].findIndex(function(x) { x == 2; });// Returns an index value of 1.
7、forEach 方法
为数组中的每个元素执行指定操作。
array1.forEach(callbackfn[, thisArg])
回调函数
function callbackfn(value, index, array1)
// Define the object that contains the callback function.var obj = { showResults: function(value, index) { // Call calcSquare by using the this value. var squared = this.calcSquare(value); document.write("value: " + value); document.write(" index: " + index); document.write(" squared: " + squared); document.write("<br />"); }, calcSquare: function(x) { return x * x }};// Define an array.var numbers = [5, 6];// Call the showResults callback function for each array element.// The obj is the this value within the // callback function.numbers.forEach(obj.showResults, obj);// Embed the callback function in the forEach statement.// The obj argument is the this value within the obj object.// The output is the same as for the previous statement.numbers.forEach(function(value, index) { this.showResults(value, index) }, obj);// Output:// value: 5 index: 0 squared: 25// value: 6 index: 1 squared: 36// value: 5 index: 0 squared: 25// value: 6 index: 1 squared: 36
8、indexOf 方法
返回某个值在数组中的第一个匹配项的索引。
array1.indexOf(searchElement[, fromIndex])
// Create an array. (The elements start at index 0.)var ar = ["ab", "cd", "ef", "ab", "cd"];// Determine the first location of "cd".document.write(ar.indexOf("cd") + "<br/>");// Output: 1// Find "cd" starting at index 2.document.write(ar.indexOf("cd", 2) + "<br/>");// Output: 4// Find "gh" (which is not found).document.write (ar.indexOf("gh")+ "<br/>");// Output: -1// Find "ab" with a fromIndex argument of -2.// The search starts at index 3, which is the array length plus -2.document.write (ar.indexOf("ab", -2) + "<br/>");// Output: 3
9、join 方法
添加由指定分隔符字符串分隔的数组的所有元素
arrayObj.join([separator])
备注:如果数组的任一元素为 undefined 或 null,则该元素将被视为空字符串。
var a, b;a = new Array(0,1,2,3,4);b = a.join("-");document.write(b);// Output:// 0-1-2-3-4
10、keys 方法
返回一个迭代器,它能返回数组的索引值。
arrayObj.keys();
var k = [“a”, “b”, “c”].keys();
// k.next().value == 0
// k.next().value == 1
// k.next().value == 2
11、lastIndexOf 方法
返回指定的值在数组中的最后一个匹配项的索引。
array1.lastIndexOf(searchElement[, fromIndex])
// Create an array.var ar = ["ab", "cd", "ef", "ab", "cd"];// Determine the first location, in descending order, of "cd".document.write(ar.lastIndexOf("cd") + "<br/>");// Output: 4// Find "cd" in descending order, starting at index 2.document.write(ar.lastIndexOf("cd", 2) + "<br/>");// Output: 1// Search for "gh" (which is not found).document.write(ar.lastIndexOf("gh")+ "<br/>");// Output: -1// Find "ab" with a fromIndex argument of -3.// The search in descending order starts at index 3,// which is the array length minus 2.document.write(ar.lastIndexOf("ab", -3) + "<br/>");// Output: 0
11、map 方法
对数组的每个元素调用定义的回调函数并返回包含结果的数组。
array1.map(callbackfn[, thisArg])
// Define an object that contains a divisor property and// a remainder function.var obj = { divisor: 10, remainder: function (value) { return value % this.divisor; }}// Create an array.var numbers = [6, 12, 25, 30];// Get the remainders.// The obj argument specifies the this value in the callback function.var result = numbers.map(obj.remainder, obj);document.write(result);// Output:// 6,2,5,0
12、pop 方法
从数组中移除最后一个元素并返回该元素。
arrayObj.pop( )
var number;var my_array = new Array();my_array.push (5, 6, 7);my_array.push (8, 9);number = my_array.pop();while (number != undefined) { document.write (number + " "); number = my_array.pop(); }// Output: 9 8 7 6 5
13、push 方法
将新元素追加到一个数组中,并返回新的数组长度。
arrayObj.push([item1 [item2 [… [itemN ]]]])
var number; var my_array = new Array(); my_array.push(5, 6, 7); my_array.push(8, 9); console.log(my_array);
14、reduce 方法
对数组中的所有元素调用指定的回调函数。该回调函数的返回值为累积结果,并且此返回值在下一次调用该回调函数时作为参数提供。
回调函数语法
function callbackfn(previousValue, currentValue, currentIndex, array1)
function Process(previousArray, currentValue) { // If currentValue is between 1 and 10, // append currentValue to the array. var nextArray; if (currentValue >= 1 && currentValue <= 10) nextArray = previousArray.concat(currentValue); else nextArray = previousArray; // If this is not the last call by the reduce method, // the returned array is previousArray on the next call. // If this is the last call by the reduce method, the // returned array is the return value of the reduce method. return nextArray;}// Create an array.var numbers = [20, 1, -5, 6, 50, 3];// Call the reduce method, starting with an initial empty array.var emptyArray = new Array();var resultArray = numbers.reduce(Process, emptyArray);document.write("result array=" + resultArray);// Output:// result array=1,6,3
15、reduceRight 方法
按降序顺序对数组中的所有元素调用指定的回调函数。该回调函数的返回值为累积结果,并且此返回值在下一次调用该回调函数时作为参数提供。
array1.reduceRight(callbackfn[, initialValue])
回调函数
function callbackfn(previousValue, currentValue, currentIndex, array1)
function Process2(previousArray, currentValue) { // If currentValue is between 1 and 10, // append currentValue to the array. var nextArray; if (currentValue >= 1 && currentValue <= 10) nextArray = previousArray.concat(currentValue); else nextArray = previousArray; // If this is not the last call by the reduceRight method, // the returned array is previousArray on the next call. // If this is the last call by the reduceRight method, the // returned array is the return value of the reduceRight method. return nextArray;}// Create an array.var numbers = [20, 1, -5, 6, 50, 3];// Call the reduceRight method, starting with an empty array.var emptyArray = new Array();var resultArray = numbers.reduceRight(Process2, emptyArray);document.write("result array=" + resultArray);// Output:// result array=3,6,1
16、reverse 方法
反转 Array 中的元素
arrayObj.reverse()
var arr = new Array(0,1,2,3,4); var reverseArr = arr.reverse();document.write(reverseArr);// Output:// 4,3,2,1,0
17、shift 方法
从数组中移除第一个元素并将返回该元素。
arrayObj.shift( )
var arr = new Array(10, 11, 12);while (arr.length > 0) { var i = arr.shift(); document.write (i.toString() + " "); }// Output: // 10 11 12
18、slice 方法
返回一个数组中的一部分。
arrayObj.slice(start, [end])
var origArray = [3, 5, 7, 9];var newArray = origArray. slice(0, -1);document.write(origArray);document.write("<br/>");newArray = origArray. slice(-2);document.write(newArray);// Output:// 3,5,7,9// 7,9
19、sort 方法
对 Array 排序。
arrayobj.sort(sortFunction)
备注:如果所传递的第一个参数小于第二个参数,则返回负值。
如果两个参数相等,则返回零。
如果第一个参数大于第二个参数,则返回正值。
var a = new Array(4, 11, 2, 10, 3, 1);var b = a.sort();document.write(b);document.write("<br/>");// This is ASCII character order.// Output: 1,10,11,2,3,4)// Sort the array elements with a function that compares array elements.b = a.sort(CompareForSort);document.write(b);document.write("<br/>");// Output: 1,2,3,4,10,11.// Sorts array elements in ascending order numerically.function CompareForSort(first, second){ if (first == second) return 0; if (first < second) return -1; else return 1; }
20、splice 方法
从一个数组中移除元素,如有必要,在所移除元素的位置上插入新元素,并返回所移除的元素。
arrayObj.splice(start, deleteCount, [item1[, item2[, … [,itemN]]]])
var arr = new Array("4", "11", "2", "10", "3", "1");arr.splice(2, 2, "21", "31");document.write(arr);// Output: 4,11,21,31,3,1
21、unshift 方法
在数组的开头插入新元素。
arrayObj.unshift([item1[, item2 [, … [, itemN]]]])
var ar = new Array();ar.unshift(10, 11);ar.unshift(12, 13, 14);document.write(ar.toString());// Output: 12,13,14,10,11
21、values 方法
返回一个迭代器,它返回数组的值
arrayObj.values();
var v = ["a", "b", "c"].values();// v.next().value == "a"// v.next().value == "b"// v.next().value == "c"
- js中Array和Object学习
- js 中有趣的Object 和Array
- js中array-like object
- JS数组”(array)和“对象”(object)
- 引用类型之Object和Array学习
- js中数组(array)和对象(object)的区别
- Javascript 中 Array和Object的关系
- JS Array Object
- js 中function 和Object
- js区分Object和Array的六种方法
- js Array Object 操作大全
- js 数据类型 Array,Object,RegExp
- js中Object常用方法和属性
- js 中new Object 和 {}的区别
- 在JS中object和字符串转换
- js中Object常用方法和属性
- js中Object常用方法和属性
- javascript中的Array和Object
- 平面上的点和线——Point类、Line类 (VII)
- 寻找一种易于理解的一致性算法(扩展版)
- xnamath.h 报错: 在标识符“XMConvertToRadians”的前面
- 编写一个递归方法,它返回数N的二进制表示中1的个数
- bzoj2081: [Poi2010]Beads
- js中Array和Object学习
- java线程池
- Android Studio自用插件小记
- SFP电口模块安装/移除指导
- 什么才是正确的原型设计?
- Android Studio项目中新建assets文件的方法
- 1120. Friend Numbers 解析
- 数据库E-R图基础
- 蓝桥杯