gpt4 book ai didi

javascript - 如何确定包含对象的两个数组之间的差异

转载 作者:行者123 更新时间:2023-11-30 19:04:04 27 4
gpt4 key购买 nike

我有一个函数可以对数据库进行 axios 调用,然后将结果推送到我之前声明的空数组中。我想查看 staffId 和 Id 之间是否存在差异,以便继续在数据库中创建新用户。我遇到的问题是确定阵列中的差异。

let userBox = []
let newUserBox = []


checkforNewUser (e) {
axios.all([
axios.get('/updateUserList'),
axios.get('/userInformation')
])
.then(axios.spread((newUsers, currentUsers) => {
// do something with both responses
userBox.push(newUsers.data)
newUserBox.push(currentUsers.data)
})).then(
console.log(userBox),
console.log(newUserBox),
).then(//a new call to create a user if the StaffId is not currently an ID in the userbox);


}

当记录数据时,响应如下:

    0: Array(9)
0: {staffID: 1, dept: "Junior Web Developer", email: "manasaN@tpionline.com", name: "manasa", password: "$2y$10$/zYS7OhzwdLOi6Slzc3Rv.aJdy1J6MjnLCN24GmZ3rMHWUS"}
1: {staffID: 2, dept: "Web Developer", email: "juliof@tpionline.net", name: "Julio Fajardo", password: "$2y$10$MphAC8aRY2uzs1ZEduvCqK1/1nd7t0KukEkvGbx5Y4van.Da6"}
2: {staffID: 3, dept: "Fake Title", email: "johnf@tpionline.com", name: "John Fester", password: "$2y$10$/oa/ESQmhhc5WvxrQKoUU.PjdZY5FccilGfCUL5eyeOU5IDSa"}
3: {staffID: 5, dept: "Fake Title", email: "fakeguy@gmail.net", name: "Test", password: "$2y$10$VWnnw5gRTN2absWos04fsOixW6dbFqsFZL8oPiJm"}
4: {staffID: 6, dept: "Conf room", email: "conf2@gmail.com", name: "Conf 1st floor", password: "$2y$10$KAZa1k2Sd/rg6dnVeqw4duB/XbAdze6Zle3ZhcMaRphloG."}
5: {staffID: 8, dept: "Fake Title", email: "newguy@gmail.net", name: "New guy", password: ""}
6: {staffID: 9, dept: "Fake Title", email: "nguy@gmail.net", name: "New Guy2", password: ""}
7: {staffID: 20, dept: "Erwing", email: "erwingh@tpionline.com", name: "Erwing Hernandez", password: "$2y$10$RWgOBbUHojgmyk3behYhd.kzdFEiH.6d70pS5q5Gf3ZYEa"}
8: {staffID: 12, dept: "Conf room", email: "cristinab@tpionline.com", name: "Cristina Bermudez", password: "$2y$10$92I6fSJY6OW001cdaeF5tdHBjWHBmoCvX1RnfhTspjpBa"}


0: {id: 1, firstName: "Elton", lastName: "Frederick", departmentId: 1, admin: true, …}
1: {id: 2, firstName: "Julio", lastName: "Probando", departmentId: 1, admin: false, …}
2: {id: 3, firstName: "Corey", lastName: "Chris", departmentId: 2, admin: false, …}
3: {id: 4, firstName: "Daisy", lastName: "Ramos", departmentId: 3, admin: false, …}
4: {id: 5, firstName: "Test", lastName: "User", departmentId: 0, admin: false, …}
5: {id: 6, firstName: "Tes2t", lastName: "Use2r", departmentId: 3, admin: false, …}
6: {id: 7, firstName: "Air Car Hotel", lastName: "Nunca", departmentId: 4, admin: false, …}
7: {id: 8, firstName: "Jenn", lastName: "Lee", departmentId: 4, admin: false, …}
8: {id: 9, firstName: "Reyna", lastName: "Pagan", departmentId: 5, admin: false, …}
9: {id: 10, firstName: "Ameilia", lastName: "Rodriguez", departmentId: 6, admin: false, …}

如果没有与 ID 匹配的员工 ID,则表示用户不在数据库中,需要在其他端点创建。当我尝试映射两者的 IDS 并比较它们时,我遇到了问题。解决这个问题的最佳方法是什么?

最佳答案

您可以将所有 ID 添加到一个对象中,并循环新用户以查找不同的 ID。这样你就不会使用嵌套循环,如果你正在使用大型数据库,这是一个加号。

const userBox = [ {staffID: 1, dept: "Junior Web Developer", email: "manasaN@tpionline.com", name: "manasa", password: "$2y$10$/zYS7OhzwdLOi6Slzc3Rv.aJdy1J6MjnLCN24GmZ3rMHWUS"},
{staffID: 2, dept: "Web Developer", email: "juliof@tpionline.net", name: "Julio Fajardo", password: "$2y$10$MphAC8aRY2uzs1ZEduvCqK1/1nd7t0KukEkvGbx5Y4van.Da6"},
{staffID: 3, dept: "Fake Title", email: "johnf@tpionline.com", name: "John Fester", password: "$2y$10$/oa/ESQmhhc5WvxrQKoUU.PjdZY5FccilGfCUL5eyeOU5IDSa"},
{staffID: 5, dept: "Fake Title", email: "fakeguy@gmail.net", name: "Test", password: "$2y$10$VWnnw5gRTN2absWos04fsOixW6dbFqsFZL8oPiJm"},
{staffID: 6, dept: "Conf room", email: "conf2@gmail.com", name: "Conf 1st floor", password: "$2y$10$KAZa1k2Sd/rg6dnVeqw4duB/XbAdze6Zle3ZhcMaRphloG."},
{staffID: 8, dept: "Fake Title", email: "newguy@gmail.net", name: "New guy", password: ""},
{staffID: 9, dept: "Fake Title", email: "nguy@gmail.net", name: "New Guy2", password: ""},
{staffID: 20, dept: "Erwing", email: "erwingh@tpionline.com", name: "Erwing Hernandez", password: "$2y$10$RWgOBbUHojgmyk3behYhd.kzdFEiH.6d70pS5q5Gf3ZYEa"},
{staffID: 12, dept: "Conf room", email: "cristinab@tpionline.com", name: "Cristina Bermudez", password: "$2y$10$92I6fSJY6OW001cdaeF5tdHBjWHBmoCvX1RnfhTspjpBa"} ]


const newUserBox = [ {id: 1, firstName: "Elton", lastName: "Frederick", departmentId: 1, admin: true},
{id: 2, firstName: "Julio", lastName: "Probando", departmentId: 1, admin: false},
{id: 3, firstName: "Corey", lastName: "Chris", departmentId: 2, admin: false},
{id: 4, firstName: "Daisy", lastName: "Ramos", departmentId: 3, admin: false},
{id: 5, firstName: "Test", lastName: "User", departmentId: 0, admin: false},
{id: 6, firstName: "Tes2t", lastName: "Use2r", departmentId: 3, admin: false},
{id: 7, firstName: "Air Car Hotel", lastName: "Nunca", departmentId: 4, admin: false},
{id: 8, firstName: "Jenn", lastName: "Lee", departmentId: 4, admin: false},
{id: 9, firstName: "Reyna", lastName: "Pagan", departmentId: 5, admin: false},
{id: 10, firstName: "Ameilia", lastName: "Rodriguez", departmentId: 6, admin: false}]

let userIds = {};
let newUsers = [];

// Save all user Ids in object
for (let user of userBox) {
userIds[user.staffID] = user.staffID;
}

// Compare new users with previous user ids and push new users in an array
for (let newUser of newUserBox) {
if (!userIds[newUser.id]) {
newUsers.push(newUser);
}
}
console.log(newUsers);

关于javascript - 如何确定包含对象的两个数组之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59180710/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com