Heim >Web-Frontend >js-Tutorial >Prototype使用指南之array.js_prototype

Prototype使用指南之array.js_prototype

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 19:21:191140Durchsuche
$A = Array.from(iterable): 将iterable转化为数组,如果iterable定义了toArray方法,就调用这个方法,否则利用iterable的length属性进行枚举, 如果iterable没有length属性的话就返回空数组[]

 Array对象除了扩展Enumerable对象的方法外,另外扩展了如下的几个方法,

注意以下方法除了clear外都不改变原来数组,而是返回一个新数组:

 clear(): 清除数组,利用arr.length=0

first(): 返回第一个元素

 last():返回最后一个元素

compact(): 去除数组中值为null或undefined的元素

flatten(): 将数组扁平化,例如[3,4,[6,7]]变为[3,4,6,7]

without():
去除指定的元素, 可以指定多个值, 例如[4,56,7,8].without(4,7) 返回[56,8]

indexOf(object): 返回指定的元素在数组中的索引,不包含则返回-1

reverse(inline):Array内置函数reverse的增强,当inline为true时,跟内置的reverse函数效果一样,改变原数组的值,否则不改变原来的值

reduce(): 如果数组只有一个元素,则返回这个元素,否则返回数组本身

uniq(): 返回没有重复元素的数组

clone(): 返回一个跟数组相同的数组,Array中的toArray方法覆盖了Enumerable中的toArray方法,指向了这个方法

 inspect(): 跟数组的toString方法类似,返回对象的字符串表示,例如[2,3].inspect() 返回 "[2,3]" 
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn