【问题标题】:Manipulating two or more arrays into one array in javascript在javascript中将两个或多个数组操作为一个数组
【发布时间】:2022-01-06 05:50:33
【问题描述】:

我正在从我的服务器获取这组数组:

{ “表名”:[ "广告配置", "api_info", "数据库配置", “对话流”, “雇员”, "ldap_details", "license_details", “登录详细信息”, "maggie_user", "邮件配置", “角色管理”, "server_details", "票务工具", “用例详细信息”, "user_tbl", “人们” ],
“创造”:[ "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:53+05:30", “2021-12-30 10:28:59+05:30” ],
“修改日期”:[ "2021-12-30 12:52:59+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-30 10:42:58+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:53+05:30", “2021-12-30 10:28:59+05:30” ] }

我想实现这个:

[0: {tableName:"adconfig",creation:"2022-01-05 11:28:33+05:30",modifyDate:"2022-01-05 11:28:33+05:30" },

1: {tableName:"api_info",creation:"2022-01-05 11:28:33+05:30",modifyDate:"2022-01-05 11:28:33+05:30"} ,

2: {tableName:"dbconfig",creation:"2022-01-05 11:28:33+05:30",modifyDate:"2022-01-05 11:28:33+05:30"} ,

等等........]

我尝试创建 3 个数组:

tableName: Array(15) 0:“adconfig” 1:“api_info” 2:“dbconfig” 3:“dialog_flow” 4:“employees” 5:“ldap_details” 6:“license_details” 7:“login_details” 8 :“maggie_user”9:“mailconfig”10:“role_management”11:“server_details”12:“ticketingtool”13:“use_case_details”14:“user_tbl”

创建:数组(15)0:“2022-01-05 11:28:33+05:30”1:“2022-01-05 11:28:33+05:30”2:“2022- 01-05 11:28:33+05:30" 3:"2022-01-05 11:28:33+05:30" 4:"2022-01-05 11:28:33+05:30" 5 : "2022-01-05 11:28:33+05:30" 6: "2022-01-05 11:28:33+05:30" 7: "2022-01-05 11:28:33+05 :30" 8:"2022-01-05 11:28:33+05:30" 9:"2022-01-05 11:28:33+05:30" 10:"2022-01-05 11:28 :33+05:30" 11:"2022-01-05 11:28:34+05:30" 12:"2022-01-05 11:28:34+05:30" 13:"2022-01- 05 11:28:34+05:30" 14:"2022-01-05 11:28:34+05:30"

修改日期:数组(15)0:“2022-01-05 11:28:33+05:30”1:“2022-01-05 11:28:33+05:30”2:“2022 -01-05 11:28:33+05:30" 3:"2022-01-05 11:28:33+05:30" 4:"2022-01-05 11:28:33+05:30" 5:“2022-01-05 11:28:33+05:30”6:“2022-01-05 11:28:33+05:30”7:“2022-01-05 11:28:33+ 05:30" 8:"2022-01-05 11:33:18+05:30" 9:"2022-01-05 11:28:33+05:30" 10:"2022-01-05 11: 33:18+05:30" 11:"2022-01-05 11:28:34+05:30" 12:"2022-01-05 11:28:34+05:30" 13:"2022-01 -05 11:33:19+05:30" 14:"2022-01-05 11:28:34+05:30" 长度:15

然后我尝试加入这三个以获得我想要的但无法获得的东西,如果有人提供任何解决方案,我将非常感激。很抱歉格式问题,因为我在这里很新,所以请原谅我提前谢谢:)

【问题讨论】:

    标签: javascript arrays ecmascript-6


    【解决方案1】:

    所以一个对象中有3个数组,你想zip它们;每个项目在数组中的索引相同,并且所有三个数组的长度相同。

    const zipped = []
    
    for(const i=0; i<response.tableName.length; i++) {
      zipped.push({
        tableName: response.tableName[i],
        creation: response.creation[i],
        modifyDate: response.modifyDate[i],
      })
    }
    

    【讨论】:

      【解决方案2】:

      这应该会产生正确的结果。

      const serverData = { 
        "tableName": [ "adconfig", "api_info", "dbconfig", "dialog_flow", "employees", "ldap_details", "license_details", "login_details", "maggie_user", "mailconfig", "role_management", "server_details", "ticketingtool", "use_case_details", "user_tbl", "people" ],
        "creation": [ "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:53+05:30", "2021-12-30 10:28:59+05:30" ],
        "modifyDate": [ "2021-12-30 12:52:59+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-30 10:42:58+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:53+05:30", "2021-12-30 10:28:59+05:30" ] 
      };
      
      const listLen = serverData["tableName"].length;
      const results = [...Array(listLen)].map((_, i) => ({
          tableName: serverData.tableName[i],
          creation: serverData.creation[i],
          modifyDate: serverData.modifyDate[i]
      }));
      
      console.log(results);

      【讨论】:

        【解决方案3】:

        考虑到每个数组的长度相同并且从服务器接收到“响应”,这里有一个单行解决方案:-

        let result = response.tableName.map((name,i) => {
            return {name, response.creation[i], response.modifyDate[i]}
        })
        

        【讨论】:

          【解决方案4】:

          在其中一个数组上使用map,并根据索引从其他数组中组合

          const data = { 
            "tableName": [ "adconfig", "api_info", "dbconfig", "dialog_flow", "employees", "ldap_details", "license_details", "login_details", "maggie_user", "mailconfig", "role_management", "server_details", "ticketingtool", "use_case_details", "user_tbl", "people" ],
            "creation": [ "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:53+05:30", "2021-12-30 10:28:59+05:30" ],
            "modifyDate": [ "2021-12-30 12:52:59+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-30 10:42:58+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:52+05:30", "2021-12-29 18:11:53+05:30", "2021-12-30 10:28:59+05:30" ] 
          };
          
          const { tableName, creation, modifyDate } = data;
          const output = tableName.map((tableName, i) => ({
            tableName,
            creation: creation[i],
            modifyDate: modifyDate[i]
          }))
          
          console.log(output);

          【讨论】:

            猜你喜欢
            • 2015-10-21
            • 2012-05-10
            • 1970-01-01
            • 2020-10-11
            • 1970-01-01
            • 1970-01-01
            • 2015-07-31
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多