浅谈Vue中key取值影响过渡效果和动画效果(代码详解)

lxf2023-03-11 09:25:01

其他回答《浅谈Vue中key取值影响过渡效果和动画效果(代码详解)》中,来给大家了解一下Vue中key选值危害过渡效果和动态效果。下边这篇来给大家了解一下JS中二维数组reduce方式,有一定的实用价值,感兴趣的小伙伴可以参考一下。

浅谈Vue中key取值影响过渡效果和动画效果(代码详解)

含意

reduce()方式对累加器和二维数组中的每个原素(从左往右)运用一个函数,把它降低为单独值。

词法

arr.reduce(callback[, initialValue])

主要参数

callback实行二维数组中每一个系数的函数公式,包括四个主要参数:accumulator累加器累积调整的传参;这是上一次启用调整时返回累计值,或initialValue(如下所示)。

currentValue

二维数组中正在维护元素。currentIndex可选择

二维数组中正在维护的现阶段元素检索。 假如带来了initialValue,则编号为 0,不然为检索为 1。array可选择

启用reduce的二维数组initialValue可选择

作为第一个启用callback的第一个主要参数数值。要是没有给予初值,则将使用二维数组里的第一个元素。在没有任何初值的空数组上启用reduce将出错。Link to section传参函数公式总计处理结论

事例

求二维数组[1,2,3,4,5]里所有值总和

// 1 赋值求合
let count = 0;
let arr = [1, 2, 3, 4, 5];
for (let i = 0; i < arr.length; i  ) {
  count  = arr[i];
}
console.log(count);
// output 15

// 2 eval
let count = eval([1, 2, 3, 4, 5].join(" "));
console.log(count);
// output 15

// 3 reduce
let count = [1, 2, 3, 4, 5].reduce((a, b) => a   b);
console.log(count);
// output 15

将二维数组转化为一维

var flattened = [
  [0, 1],
  [2, 3],
  [4, 5],
].reduce((acc, cur) => acc.concat(cur), []);

测算二维数组中每一个原素发生次数

var names = ["Alice", "Bob", "Tiff", "Bruce", "Alice"];

var countedNames = names.reduce(function (allNames, name) {
  if (name in allNames) {
    allNames[name]  ;
  } else {
    allNames[name] = 1;
  }
  return allNames;
}, {});
// countedNames is:
// { 'Alice': 2, 'Bob': 1, 'Tiff': 1, 'Bruce': 1 }

应用拓展操作符和initialValue关联包含于对象数组里的二维数组

// friends - an array of objects
// where object field "books" - list of favorite books
var friends = [
  {
    name: "Anna",
    books: ["Bible", "Harry Potter"],
    age: 21,
  },
  {
    name: "Bob",
    books: ["War and peace", "Romeo and Juliet"],
    age: 26,
  },
  {
    name: "Alice",
    books: ["The Lord of the Rings", "The Shining"],
    age: 18,
  },
];

// allbooks - list which will contain all friends' books  
// additional list contained in initialValue
var allbooks = friends.reduce(
  function (prev, curr) {
    return [...prev, ...curr.books];
  },
  ["Alphabet"]
);

// allbooks = [
//   'Alphabet', 'Bible', 'Harry Potter', 'War and peace',
//   'Romeo and Juliet', 'The Lord of the Rings',
//   'The Shining'
// ]

数组去重

let arr = [1, 2, 1, 2, 3, 5, 4, 5, 3, 4, 4, 4, 4];
let result = arr.sort().reduce((init, current) => {
  if (init.length === 0 || init[init.length - 1] !== current) {
    init.push(current);
  }
  return init;
}, []);
console.log(result); //[1,2,3,4,5]

二维数组取最大值和极小值

let data = [1, 4, 2, 2, 4, 5, 6, 7, 8, 8, 9, 10];
//取最小值
let min = data.reduce((x, y) => (x > y ? y : x));
//取最大值
let max = data.reduce((x, y) => (x > y ? x : y));

ES5的实现

if (!Array.prototype.reduce) {
  Object.defineProperty(Array.prototype, "reduce", {
    value: function (callback /*, initialValue*/) {
      if (this === null) {
        throw new TypeError(
          "Array.prototype.reduce "   "called on null or undefined"
        );
      }
      if (typeof callback !== "function") {
        throw new TypeError(callback   " is not a function");
      }

      // 1. Let O be ? ToObject(this value).
      var o = Object(this);

      // 2. Let len be ? ToLength(? Get(O, "length")).
      var len = o.length >>> 0;
      // >>表示的是带符号的偏移:依照二进制把数据偏移特定多位,上位如符号位为正补零,符号位负补一,底位立即清除
      //  >>>表明无符号的偏移:依照二进制把数据偏移特定多位,上位立即补零,低偏移除。

      // Steps 3, 4, 5, 6, 7
      var k = 0;
      var value;

      if (arguments.length >= 2) {
        value = arguments[1];
      } else {
        while (k < len && !(k in o)) {   k  ;
        }

        // 3. 长度为0 且初值不会有 抛出异常
        if (k >= len) {
          throw new TypeError(
            "Reduce of empty array "   "with no initial value"
          );
        }
        value = o[k  ];
      }

      // 8. Repeat, while k < len
      while (k < len) {
        // a. Let Pk be ! ToString(k).
        // b. Let kPresent be ? HasProperty(O, Pk).
        // c. If kPresent is true, then
        //    i.  Let kValue be ? Get(O, Pk).
        //    ii. Let accumulator be ? Call(
        //          callbackfn, undefined,
        //          « accumulator, kValue, k, O »).
        if (k in o) {
          value = callback(value, o[k], k, o);
        }

        // d. Increase k by 1.
        k  ;
      }

      // 9. Return accumulator.
      return value;
    },
  });
}

强烈推荐学习培训:JavaScript视频教学

以上就是关于深层次分析JS中二维数组reduce方式(附编码)的具体内容,大量欢迎关注AdminJS其他类似文章!