当前位置: 首页 > news >正文

js三层数组循环遍历(笛卡尔积)运算,并生成顺序

笛卡尔乘积是指在数学中,两个集合X和Y的笛卡尔积(Cartesian product),又称直积,表示为X × Y,第一个对象是X的成员而第二个对象是Y的所有可能有序对的其中一个成员 [1]  。

假设集合A={a, b},集合B={0, 1, 2},则两个集合的笛卡尔积为{(a, 0), (a, 1), (a, 2), (b, 0), (b, 1), (b, 2)}。

js实现三层数组循环遍历(笛卡尔积)运算,并生成顺序。详细完整代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>笛卡尔积 遍历并生成顺序</title>
</head>
<body>
  <script>
    let arr1 = ['test11', 'test12', 'test13'], 
    arr2 = ['test21', 'test22', 'test23'], 
    arr3 = ['test31', 'test32'], 
    arr4 = ['test41', 'test42', 'test43'];
    let apiList_ = [], i1 = 0, i2 = 0;
    arr1.forEach((id, index1) => {
      console.log(i2);
      arr2.forEach((item2, index2) => {
        arr3.map((item, index) => {
          i1 = i2 + index + 1;
          apiList_.push({
            SORT: i1,  // arr2第一遍结束为2  arr2第二遍结束为7
            name: id + '_' + item2 + '_' + item
          });
        });
        
        arr4.map((item, index) => {
          i2 = i1 + index + 1;  
          apiList_.push({
            SORT: i2,  // arr2第一遍结束为5  arr2第二遍结束为10
            name: id + '_' + item2 + '_' + item
          });
        });
      });
    });
    console.log(apiList_);
  </script>
</body>
</html>

 输出结果如下:

 完整结果:

[
    {
        "SORT": 1,
        "name": "test11_test21_test31"
    },
    {
        "SORT": 2,
        "name": "test11_test21_test32"
    },
    {
        "SORT": 3,
        "name": "test11_test21_test41"
    },
    {
        "SORT": 4,
        "name": "test11_test21_test42"
    },
    {
        "SORT": 5,
        "name": "test11_test21_test43"
    },
    {
        "SORT": 6,
        "name": "test11_test22_test31"
    },
    {
        "SORT": 7,
        "name": "test11_test22_test32"
    },
    {
        "SORT": 8,
        "name": "test11_test22_test41"
    },
    {
        "SORT": 9,
        "name": "test11_test22_test42"
    },
    {
        "SORT": 10,
        "name": "test11_test22_test43"
    },
    {
        "SORT": 11,
        "name": "test11_test23_test31"
    },
    {
        "SORT": 12,
        "name": "test11_test23_test32"
    },
    {
        "SORT": 13,
        "name": "test11_test23_test41"
    },
    {
        "SORT": 14,
        "name": "test11_test23_test42"
    },
    {
        "SORT": 15,
        "name": "test11_test23_test43"
    },
    {
        "SORT": 16,
        "name": "test12_test21_test31"
    },
    {
        "SORT": 17,
        "name": "test12_test21_test32"
    },
    {
        "SORT": 18,
        "name": "test12_test21_test41"
    },
    {
        "SORT": 19,
        "name": "test12_test21_test42"
    },
    {
        "SORT": 20,
        "name": "test12_test21_test43"
    },
    {
        "SORT": 21,
        "name": "test12_test22_test31"
    },
    {
        "SORT": 22,
        "name": "test12_test22_test32"
    },
    {
        "SORT": 23,
        "name": "test12_test22_test41"
    },
    {
        "SORT": 24,
        "name": "test12_test22_test42"
    },
    {
        "SORT": 25,
        "name": "test12_test22_test43"
    },
    {
        "SORT": 26,
        "name": "test12_test23_test31"
    },
    {
        "SORT": 27,
        "name": "test12_test23_test32"
    },
    {
        "SORT": 28,
        "name": "test12_test23_test41"
    },
    {
        "SORT": 29,
        "name": "test12_test23_test42"
    },
    {
        "SORT": 30,
        "name": "test12_test23_test43"
    },
    {
        "SORT": 31,
        "name": "test13_test21_test31"
    },
    {
        "SORT": 32,
        "name": "test13_test21_test32"
    },
    {
        "SORT": 33,
        "name": "test13_test21_test41"
    },
    {
        "SORT": 34,
        "name": "test13_test21_test42"
    },
    {
        "SORT": 35,
        "name": "test13_test21_test43"
    },
    {
        "SORT": 36,
        "name": "test13_test22_test31"
    },
    {
        "SORT": 37,
        "name": "test13_test22_test32"
    },
    {
        "SORT": 38,
        "name": "test13_test22_test41"
    },
    {
        "SORT": 39,
        "name": "test13_test22_test42"
    },
    {
        "SORT": 40,
        "name": "test13_test22_test43"
    },
    {
        "SORT": 41,
        "name": "test13_test23_test31"
    },
    {
        "SORT": 42,
        "name": "test13_test23_test32"
    },
    {
        "SORT": 43,
        "name": "test13_test23_test41"
    },
    {
        "SORT": 44,
        "name": "test13_test23_test42"
    },
    {
        "SORT": 45,
        "name": "test13_test23_test43"
    }
]

相关文章:

  • ERROR in ./src/.umi/core/routes.ts 402:19-404:109Module not found: Error: [CaseSensitivePathsPlugin
  • 响应式布局
  • GIT安装与使用
  • 初识微信小程序
  • 初步微信小程序
  • 模板引用
  • template标签与小程序数据加载报错问题
  • 上拉加载
  • 组件与组件通信
  • 组建的引入与传参
  • 小程序游戏请求
  • 小程序登录
  • 云开发增删改
  • 2021-02-06
  • 小程序框架初识和mpvue的安装步骤
  • 03Go 类型总结
  • ES2017异步函数现已正式可用
  • react-native 安卓真机环境搭建
  • STAR法则
  • 紧急通知:《观止-微软》请在经管柜购买!
  • 经典排序算法及其 Java 实现
  • 如何优雅的使用vue+Dcloud(Hbuild)开发混合app
  • 数组大概知多少
  • 微信如何实现自动跳转到用其他浏览器打开指定页面下载APP
  • 【运维趟坑回忆录】vpc迁移 - 吃螃蟹之路
  • Java数据解析之JSON
  • 不要一棍子打翻所有黑盒模型,其实可以让它们发挥作用 ...
  • ​DB-Engines 11月数据库排名:PostgreSQL坐稳同期涨幅榜冠军宝座
  • #Ubuntu(修改root信息)
  • #中的引用型是什么意识_Java中四种引用有什么区别以及应用场景
  • (LeetCode 49)Anagrams
  • (八)c52学习之旅-中断实验
  • (附源码)springboot炼糖厂地磅全自动控制系统 毕业设计 341357
  • (论文阅读31/100)Stacked hourglass networks for human pose estimation
  • (十七)Flask之大型项目目录结构示例【二扣蓝图】
  • (图)IntelliTrace Tools 跟踪云端程序
  • (五)Python 垃圾回收机制
  • (转)AS3正则:元子符,元序列,标志,数量表达符
  • (转)http协议
  • .NET 4.0中的泛型协变和反变
  • .NET BackgroundWorker
  • .NET C#版本和.NET版本以及VS版本的对应关系
  • .NET Core 项目指定SDK版本
  • .NET Core使用NPOI导出复杂,美观的Excel详解
  • .NET delegate 委托 、 Event 事件,接口回调
  • .NET/C# 利用 Walterlv.WeakEvents 高性能地中转一个自定义的弱事件(可让任意 CLR 事件成为弱事件)
  • @RequestParam,@RequestBody和@PathVariable 区别
  • @select 怎么写存储过程_你知道select语句和update语句分别是怎么执行的吗?
  • [Android开源]EasySharedPreferences:优雅的进行SharedPreferences数据存储操作
  • [Angular 基础] - 自定义指令,深入学习 directive
  • [Asp.net MVC]Bundle合并,压缩js、css文件
  • [autojs]autojs开关按钮的简单使用
  • [CSS] - 修正IE6不支持position:fixed的bug
  • [HDU5685]Problem A
  • [i.MX]飞思卡尔IMX6处理器的GPIO-IOMUX_PAD说明