some、sort、splice

来源:互联网 发布:js给字段赋值 编辑:程序博客网 时间:2024/05/22 13:35
语法
arr.some(callback[, thisArg])


实例

function isBiggerThan10(element, index, array) {  return element > 10;}[2, 5, 8, 1, 4].some(isBiggerThan10);  // false[12, 5, 8, 1, 4].some(isBiggerThan10); // true

检查数组中是否存在一个值

var fruits = ['apple', 'banana', 'mango', 'guava'];function checkAvailability(arr, val) {    return arr.some(function(arrVal) {        return val === arrVal;    });}checkAvailability(fruits, 'kela'); //falsecheckAvailability(fruits, 'banana'); //true

将任何值转换为布尔值
var TRUTHY_VALUES = [true, 'true', 1];function getBoolean(a) {    'use strict';    var value = a;        if (typeof value === 'string') {         value = value.toLowerCase().trim();    }    return TRUTHY_VALUES.some(function(t) {        return t === value;    });}getBoolean(false); // falsegetBoolean('false'); // falsegetBoolean(1); // truegetBoolean('true'); // true

源码
// Production steps of ECMA-262, Edition 5, 15.4.4.17// Reference: http://es5.github.io/#x15.4.4.17if (!Array.prototype.some) {  Array.prototype.some = function(fun/*, thisArg*/) {    'use strict';    if (this == null) {      throw new TypeError('Array.prototype.some called on null or undefined');    }    if (typeof fun !== 'function') {      throw new TypeError();    }    var t = Object(this);    var len = t.length >>> 0;    var thisArg = arguments.length >= 2 ? arguments[1] : void 0;    for (var i = 0; i < len; i++) {      if (i in t && fun.call(thisArg, t[i], i, t)) {        return true;      }    }    return false;  };}

sort语法

arr.sort([compareFunction])

数字排序

var numbers = [4, 2, 5, 1, 3];numbers.sort(function(a, b) {  return a - b;});console.log(numbers);// [1, 2, 3, 4, 5]

实例

var stringArray = ['Blue', 'Humpback', 'Beluga'];var numericStringArray = ['80', '9', '700'];var numberArray = [40, 1, 5, 200];var mixedNumericArray = ['80', '9', '700', 40, 1, 5, 200];function compareNumbers(a, b) {  return a - b;}console.log('stringArray:', stringArray.join());console.log('Sorted:', stringArray.sort());console.log('numberArray:', numberArray.join());console.log('Sorted without a compare function:', numberArray.sort());console.log('Sorted with compareNumbers:', numberArray.sort(compareNumbers));console.log('numericStringArray:', numericStringArray.join());console.log('Sorted without a compare function:', numericStringArray.sort());console.log('Sorted with compareNumbers:', numericStringArray.sort(compareNumbers));console.log('mixedNumericArray:', mixedNumericArray.join());console.log('Sorted without a compare function:', mixedNumericArray.sort());console.log('Sorted with compareNumbers:', mixedNumericArray.sort(compareNumbers));stringArray: Blue,Humpback,BelugaSorted: Beluga,Blue,HumpbacknumberArray: 40,1,5,200Sorted without a compare function: 1,200,40,5Sorted with compareNumbers: 1,5,40,200numericStringArray: 80,9,700Sorted without a compare function: 700,80,9Sorted with compareNumbers: 9,80,700mixedNumericArray: 80,9,700,40,1,5,200Sorted without a compare function: 1,200,40,5,700,80,9Sorted with compareNumbers: 1,5,9,40,80,200,700

字母排序

var items = ['réservé', 'premier', 'cliché', 'communiqué', 'café', 'adieu'];items.sort(function (a, b) {  return a.localeCompare(b);});// items is ['adieu', 'café', 'cliché', 'communiqué', 'premier', 'réservé']
排序图
// the array to be sortedvar list = ['Delta', 'alpha', 'CHARLIE', 'bravo'];// temporary array holds objects with position and sort-valuevar mapped = list.map(function(el, i) {  return { index: i, value: el.toLowerCase() };})// sorting the mapped array containing the reduced valuesmapped.sort(function(a, b) {  return +(a.value > b.value) || +(a.value === b.value) - 1;});// container for the resulting ordervar result = mapped.map(function(el){  return list[el.index];});

splice语法

array.splice(start, deleteCount[, item1[, item2[, ...]]])

实例

var myFish = ['angel', 'clown', 'mandarin', 'surgeon'];// removes 0 elements from index 2, and inserts 'drum'var removed = myFish.splice(2, 0, 'drum');// myFish is ['angel', 'clown', 'drum', 'mandarin', 'surgeon']// removed is [], no elements removed// myFish is ['angel', 'clown', 'drum', 'mandarin', 'surgeon'] // removes 1 element from index 3removed = myFish.splice(3, 1);// myFish is ['angel', 'clown', 'drum', 'surgeon']// removed is ['mandarin']// myFish is ['angel', 'clown', 'drum', 'surgeon'] // removes 1 element from index 2, and inserts 'trumpet'removed = myFish.splice(2, 1, 'trumpet');// myFish is ['angel', 'clown', 'trumpet', 'surgeon']// removed is ['drum']// myFish is ['angel', 'clown', 'trumpet', 'surgeon'] // removes 2 elements from index 0, and inserts 'parrot', 'anemone' and 'blue'removed = myFish.splice(0, 2, 'parrot', 'anemone', 'blue');// myFish is ['parrot', 'anemone', 'blue', 'trumpet', 'surgeon']// removed is ['angel', 'clown']// myFish is ['parrot', 'anemone', 'blue', 'trumpet', 'surgeon'] // removes 2 elements from index 2removed = myFish.splice(myFish.length -3, 2);// myFish is ['parrot', 'anemone', 'surgeon']// removed is ['blue', 'trumpet']

0 0