美文网首页
No.22(数据类型、对象)

No.22(数据类型、对象)

作者: 梅格Drum | 来源:发表于2018-06-23 21:31 被阅读0次

    掌握 JavaScript 中的各个数据类型、对象的概念及常用方法

    排序

    Array.prototype.sort(compareFunction)


    • 参数:接收一个compareFunction函数,如果省略的话会按照换为的字符串的各个字符的Unicode位点进行排序。
    • 如果compareFunction(a,b)大于0,a在b的后面
    • 如果compareFunction(a,b)等于0位置不发生变化
    • 如果compareFunction(a,b)小于0,a在b的前面
    • 返回值:返回排序后的数组。原数组已经被排序后的数组代替。
    //number数组类型的排序
    function compare(a, b) {
      if (a < b ) {           // 按某种排序标准进行比较, a 小于 b
        return -1;
      }
      if (a > b ) {
        return 1;
      }
      // a must be equal to b
      return 0;
    }
    //要比较数字而非字符串,比较函数可以简单的以 a 减 b,如下的函数将会将数组升序排列
    function compareNumbers(a, b) {
      return a - b;
    }
    
    //string数组类型的排序
    // 需要被排序的数组
                var list = ['Delta', 'alpha', 'CHARLIE', 'bravo'];
                
                // 对需要排序的数字和位置的临时存储
                var mapped = list.map(function(el, i) {
                  return { index: i, value: el.toLowerCase() };
                })
                
                // 按照多个值排序数组
                mapped.sort(function(a, b) {
                  //字符串比较是逐个比较ascii值得大小,比如"c"<"d";
                  if(a.value>b.value)
                  {
                    return 1;
                  }
                  if(a.value<b.value)
                  {
                    return -1;
                  }
                  else{
                    return 0;
                  }
                });
                console.log(mapped);
    

    Array.prototype.reduce(callback[, initialValue])


    reduce() 方法对累加器和数组中的每个元素(从左到右)应用一个函数,将其减少为单个值。

    • 参数:callback 执行数组中每个值的函数,包含四个参数:
    • accumulator 累加器累加回调的返回值; 它是上一次调用回调时返回的累积值,或initialValue(如下所示)。
    • currentValue 数组中正在处理的元素。
    • currentIndex(可选) 数组中正在处理的当前元素的索引。 如果提供了initialValue,则索引号为0,否则为索引为1。
    • array(可选) 调用reduce的数组
    • initialValue(可选) 用作第一个调用 callback的第一个参数的值。 如果没有提供初始值,则将使用数组中的第一个元素。 在没有初始值的空数组上调用 reduce 将报错。
    • 返回值 函数累计处理的结果
      回调函数第一次执行时,accumulator 和currentValue的取值有两种情况:调用reduce时提供initialValue,accumulator取值为initialValue,currentValue取数组中的第一个值;没有提供 initialValue,accumulator取数组中的第一个值,currentValue取数组中的第二个值。
    var sum = [0, 1, 2, 3].reduce(function (a, b) {
      return a + b;
    }, 0);
    // sum is 6
    var flattened = [[0, 1], [2, 3], [4, 5]].reduce(
      function(a, b) {
        return a.concat(b);
      },
      []
    );
    // flattened is [0, 1, 2, 3, 4, 5]
    

    Array.prototype.map()


    map() 方法创建一个新数组,其结果是该数组中的每个元素都调用一个提供的函数后返回的结果。

    语法

    let new_array = arr.map(function callback(currentValue, index, array) {
    // Return element for new_array
    }[, thisArg])

    • 参数:callback 生成新数组元素的函数,使用三个参数:
    • currentValue callback 的第一个参数,数组中正在处理的当前元素。。
    • index callback 的第二个参数,数组中正在处理的当前元素的索引。
    • array callback 的第三个参数,map 方法被调用的数组。
    • thisArg (可选) 执行 callback 函数时 使用的this 值。
    • 返回值 一个新数组,每个元素都是回调函数的结果。
      map 方法会给原数组中的每个元素都按顺序调用一次 callback 函数。
      map 不修改调用它的原数组本身(当然可以在 callback 执行时改变原数组)。
    var numbers = [1, 4, 9];
    var doubles = numbers.map(function(num) {
      return num * 2;
    });
    
    // doubles数组的值为: [2, 8, 18]
    // numbers数组未被修改: [1, 4, 9]
    var map = Array.prototype.map
    var a = map.call("Hello World", function(x) { 
      return x.charCodeAt(0); 
    })
    // a的值为[72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]
    

    相关文章

      网友评论

          本文标题:No.22(数据类型、对象)

          本文链接:https://www.haomeiwen.com/subject/uecdyftx.html