我有2个数组,其中一个数组的所有信息都需要添加到不同数组的每个记录中。例如
arrUsers ["Cesar", "John"]
和
arrTrainin ["SOP1", "SOP2","SOP3","SOP4"]
预期结果将是
arrSOPrecord [
{"User":"Cesar", "SOP":"SOP1"},
{"User":"Cesar", "SOP":"SOP2"},
{"User":"Cesar", "SOP":"SOP3"},
{"User":"Cesar", "SOP":"SOP4"},
{"User":"John", "SOP":"SOP1"},
{"User":"John", "SOP":"SOP2"},
{"User":"John", "SOP":"SOP3"},
{"User":"John", "SOP":"SOP4"}
]
我尝试将key.value
添加到变量/ s,但结果仅适用于一个用户和SOP组合。任何帮助将不胜感激
答案 0 :(得分:0)
这是你可能想做的事情:
const arrUsers = ['Cesar', 'John'];
const arrTrainin = ['SOP1', 'SOP2', 'SOP3', 'SOP4'];
const arrSOPrecord = [];
arrUsers.map(user => {
arrTrainin.map(training => {
arrSOPrecord.push({ User: user, SOP: training });
});
});
答案 1 :(得分:0)
您可以迭代arrUsers
和arrTraining
。
var arrUsers = ["Cesar", "John"],
arrTraining = ["SOP1", "SOP2", "SOP3", "SOP4"],
arrSOPrecord = [];
arrUsers.forEach(function (u) {
arrTraining.forEach(function (t) {
arrSOPrecord.push({ User: u, SOP: t });
});
});
console.log(arrSOPrecord);