我将数据库中的数据作为一个数组,如下所示。
["John", "Seth", "Eben"]["john@domain.gov.gh", "seth@domain.gov.gh", "eben@domain.gov.gh"]`["0212225252", "0201115555", "0201115556"]`它们是不同的数组,但数据是相互链接的,例如:姓名、电子邮件、电话我想将此数组重新排列为多维数组,但数组之间具有适当的关系。示例
["John", "john@domain.gov.gh", "0212225252"]
["Seth", "seth@domain.gov.gh", "0201115555"]以此类推。
我尝试过很多不同的push和merge选项,但我猜我在javascript上的技能不能胜任这项任务。生成的数据也将显示在HTML表中。这部分我已经用一些测试虚拟数据进行了整理。
任何帮助都是非常感谢的。谢谢
发布于 2020-07-20 20:51:36
使用循环来执行以下操作:
let names = ["John", "Seth", "Eben"];
let emails = ["john@domain.gov.gh", "seth@domain.gov.gh",
"eben@domain.gov.gh"];
let phones = ["0212225252", "0201115555", "0201115556"];
let matrix = [];
for (let i = 0; i < names.length; i++) {
matrix.push([names[i], emails[i], phones[i]]);
}
console.log(matrix);
发布于 2020-07-20 20:55:46
假设每个数组中的相同索引总是相关的。
let names = ["John", "Seth", "Eben"],
emails = ["john@domain.gov.gh", "seth@domain.gov.gh", "eben@domain.gov.gh"],
phones = ["0212225252", "0201115555", "0201115556"],
array = [];
for (const i in names) {
array.push([names[i], emails[i], phones[i]]);
}
console.log(array);
发布于 2020-07-20 20:59:40
const a = ["John", "Seth", "Eben"]
const b = ["john@domain.gov.gh", "seth@domain.gov.gh", "eben@domain.gov.gh"]
const c = ["0212225252", "0201115555", "0201115556"]
function mixin(a, b, c) {
const names = JSON.parse(JSON.stringify(a));
const emails = JSON.parse(JSON.stringify(b));
const tels = JSON.parse(JSON.stringify(c));
return names.map((item, index) => [item, emails[index], tels[index]])
}
console.log(mixin(a, b, c))
https://stackoverflow.com/questions/62995714
复制相似问题