zoukankan      html  css  js  c++  java
  • javascript中五种迭代方法实例

    温习一下js中的迭代方法。

    <script type="text/javascript">
      var arr = [1, 2, 3, 4, 5, 4, 3, 2, 1];

      //所有项为false,则为false
      //否则直到遍历到第一个为true的值,返回true
      //类似于数学里的‘或’
      console.log(arr.some((item, index, array) => {
        console.log('type=some,' + 'item=' + item + ',index=' + index + ',array=' + array);
        return item > 3;
      }));

      //所有项都为true,则返回true
      //否则遍历到第一个为false的值,返回false
      //类似于数学里的‘与’
      console.log(arr.every((item, index, array) => {
        console.log('type=every,' + 'item=' + item + ',index=' + index + ',array=' + array);
        return item > 3;
      }));

      //遍历完全,返回由结果为true的值组成的数组
      console.log(arr.filter((item, index, array) => {
        //console.log('type=filter,' + 'item=' + item + ',index=' + index + ',array=' + array);
        return (item > 3);

      }));

      //遍历完全,将每一项带入函数,返回由结果组成的数组
      console.log(arr.map((item, index, array) => {
        //console.log('type=filter,' + 'item=' + item + ',index=' + index + ',array=' + array);
        return (item * 3);

      }));

      //遍历完全,类似for循环
      arr.forEach((item, index, array) => {
        console.log('type=forEach,' + 'item=' + item + ',index=' + index + ',array=' + array);
      });
    </script>

  • 相关阅读:
    vue插件(还真是第一次接触)
    Vue父组件向子组件传值以及data和props的区别
    Vue v-bind与v-model的区别
    vue 异步渲染
    vue动态加载不同的组件(分内部和外部组件)
    vue自定义组件的递归
    作用域插槽模板迭代的次数,取决于组件内部独立slot的数量
    说说 Vue.js 中的 v-cloak 指令
    C语言 system
    C语言 有符号、无符号
  • 原文地址:https://www.cnblogs.com/tenfly/p/11443310.html
Copyright © 2011-2022 走看看