Home > Net >  Manipulating two or more arrays into one array in javascript
Manipulating two or more arrays into one array in javascript

Time:01-06

I am getting this set of array from my server:

{ "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" ] }

I want to achieve this:

[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"},

and so on........]

I tried creating 3 arrays as:

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"

creation: Array(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"

modify Date: Array(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" length: 15

Then i tried to join this three to get what i desired but was not able to, I would really appreciate if someone comes with any solution. Sorry for the formatting issue as i am quite new here so please pardon me Thanks in advance :)

CodePudding user response:

So 3 arrays inside an object, and you want to zip them; Each item is at same index across arrays, and all three arrays have the same length.

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],
  })
}

CodePudding user response:

This should produce the correct results.

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);

  •  Tags:  
  • Related