[英]Trying to make a new object from three exsisting objects using javascript
I have three objects each are serialized from Django-rest framework.我有三个对象,每个对象都是从 Django-rest 框架序列化的。 Each object is a array of a user in a profile.
每个对象都是配置文件中用户的数组。 Each profile needs one org_id and email to be valid.
每个配置文件都需要一个 org_id 和电子邮件才能有效。 "activeUsers" and "inactiveUsers" will never have a duplicates, each element shares the same org_id, and user_id and email will always be different.
"activeUsers" 和 "inactiveUsers" 永远不会有重复,每个元素共享相同的 org_id,并且 user_id 和 email 总是不同的。 "nonProfileUsers" are all the other profiles that exlude
“nonProfileUsers”是排除的所有其他配置文件
"activeUsers" and "inactiveUsers" org_id, note user_id and email can and will show up more then once. "activeUsers" 和 "inactiveUsers" org_id,注意 user_id 和 email 可以并且将会出现不止一次。
activeUsers = [
{ "org_id": 1, "user_id": 9, "firstName": "Joj", "lastName": "Blonde", "email": "bop@test.ca", },
{"org_id": 1, "user_id": 1, "firstName": "Tyler", "lastName": "Whitfield", "email": "tyler@atg.net", },
]
inactiveUsers = [
{ "org_id": 1, "user_id": 3, "firstName": "James", "lastName": "Bond", "email": "test@test.ca", },
{"org_id": 1, "user_id": 2, "firstName": "Chen", "lastName": "rain", "email": "posihdun@gmail.com", }
]
//user that are in different profiles
nonProfileUsers = [
{ "org_id": 2, "user_id": 2, "firstName": "Chen", "lastName": "rain", "email": "posihdun@gmail.com", },
{ "org_id": 3, "user_id": 6, "firstName": "weak", "lastName": "jdf", "email": "not@not.ca", },
{ "org_id": 3, "user_id": 2, "firstName": "Chen", "lastName": "rain", "email": "posihdun@gmail.com", },
{ "org_id": 4, "user_id": 2, "firstName": "Chen", "lastName": "rain", "email": "posihdun@gmail.com", },
{ "org_id": 3, "user_id": 3, "firstName": "James", "lastName": "Bond", "email": "test@test.ca", },
{ "org_id": 2, "user_id": 5, "firstName": "test", "lastName": "test", "email": "tester@tester.ca", },
{ "org_id": 2, "user_id": 3, "firstName": "James", "lastName": "Bond", "email": "test@test.ca", },
]
I am using java-script and I need to do a few things.我正在使用 java 脚本,我需要做一些事情。 scale down "nonProfileUsers" to remove duplicated user_id and email combination, but keep at least one org_id(which one is kept does not matter)
缩小“nonProfileUsers”以删除重复的 user_id 和 email 组合,但至少保留一个 org_id(保留哪一个无关紧要)
scaledNonProfileUsers = [
{ "org_id": 2, "user_id": 2, "firstName": "Chen", "lastName": "rain", "email": "posihdun@gmail.com", },
{ "org_id": 3, "user_id": 6, "firstName": "weak", "lastName": "jdf", "email": "not@not.ca", },
{ "org_id": 2, "user_id": 5, "firstName": "test", "lastName": "test", "email": "tester@tester.ca", },
{ "org_id": 2, "user_id": 3, "firstName": "James", "lastName": "Bond", "email": "test@test.ca", },
]
combined "activeUsers" and "inactiveUsers" -I was able to complete step 2结合了“activeUsers”和“inactiveUsers”——我能够完成第 2 步
combinedUsers= [
{ "org_id": 1, "user_id": 9, "firstName": "Joj", "lastName": "Blonde", "email": "bop@test.ca", },
{"org_id": 1, "user_id": 1, "firstName": "Tyler", "lastName": "Whitfield", "email": "tyler@atg.net", },
{ "org_id": 1, "user_id": 3, "firstName": "James", "lastName": "Bond", "email": "test@test.ca", },
{"org_id": 1, "user_id": 2, "firstName": "Chen", "lastName": "rain", "email": "posihdun@gmail.com", }
]
Make a new object from comparing "scaledNonProfileUsers" vs "combinedUsers".通过比较“scaledNonProfileUsers”与“combinedUsers”创建一个新对象。 The emails that are in "scaledNonProfileUsers" and not in "combinedUsers" will make the final object.
“scaledNonProfileUsers”中而不是“combinedUsers”中的电子邮件将成为最终对象。
finalObj= [
{ "org_id": 3, "user_id": 6, "firstName": "weak", "lastName": "jdf", "email": "not@not.ca", },
{ "org_id": 2, "user_id": 5, "firstName": "test", "lastName": "test", "email": "tester@tester.ca", },
]
I am really struggling with steps 1 and 3.我真的在第 1 步和第 3 步苦苦挣扎。
If I isolate just the email I can make a array like so如果我只隔离电子邮件,我可以像这样制作一个数组
[
0:"non@not.ca",
1:"tester@tester.ca"
]
But I need to include at least one org_id and email.但我需要至少包含一个 org_id 和电子邮件。
This code has been my approach thus far.到目前为止,这段代码一直是我的方法。
const temp_active = [...inactiveUsers, ...activeUsers,]// master list of both active/inactive
const scaledNonProfileUsers= []//isolate email and make a list of emails
for (var i = 0; i < nonProfileUsers?.length; i++) {
scaledNonProfileUsers.push(nonProfileUsers[i]?.email)
}
const combinedUsers= []//isolate email and make a list of emails
for (var i = 0; i < temp_active?.length; i++) {
combinedUsers.push(temp_active[i]?.email)
}
let combinedUsers_filter = combinedUsers.filter((c, index) => {//filter duplicated emails
return combinedUsers.indexOf(c) === index;
});
let scaledNonProfileUsers_filter = scaledNonProfileUsers.filter((c, index) => {//filter duplicate emails
return scaledNonProfileUsers.indexOf(c) === index;
});
var finalObj = [];
for (var i = 0; i < scaledNonProfileUsers_filter?.length; i++) {
if (!eleContainsInArray(combinedUsers_filter, scaledNonProfileUsers_filter[i])) {
finalObj.push(scaledNonProfileUsers_filter[i])
}
}
function eleContainsInArray(arr, element) {
if (arr != null && arr.length > 0) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] == element)
return true;
}
}
return false;
}
console.log(finalObj)
Anyone help would be much appreciated.任何帮助将不胜感激。
const activeUsers = [ { org_id: 1, user_id: 9, firstName: 'Joj', lastName: 'Blonde', email: 'bop@test.ca', }, { org_id: 1, user_id: 1, firstName: 'Tyler', lastName: 'Whitfield', email: 'tyler@atg.net', }, ] const inactiveUsers = [ { org_id: 1, user_id: 3, firstName: 'James', lastName: 'Bond', email: 'test@test.ca', }, { org_id: 1, user_id: 2, firstName: 'Chen', lastName: 'rain', email: 'posihdun@gmail.com', }, ] const nonProfileUsers = [ { org_id: 2, user_id: 2, firstName: 'Chen', lastName: 'rain', email: 'posihdun@gmail.com', }, { org_id: 3, user_id: 6, firstName: 'weak', lastName: 'jdf', email: 'not@not.ca', }, { org_id: 3, user_id: 2, firstName: 'Chen', lastName: 'rain', email: 'posihdun@gmail.com', }, { org_id: 4, user_id: 2, firstName: 'Chen', lastName: 'rain', email: 'posihdun@gmail.com', }, { org_id: 3, user_id: 3, firstName: 'James', lastName: 'Bond', email: 'test@test.ca', }, { org_id: 2, user_id: 5, firstName: 'test', lastName: 'test', email: 'tester@tester.ca', }, { org_id: 2, user_id: 3, firstName: 'James', lastName: 'Bond', email: 'test@test.ca', }, ] const filteredNonProfileUsers = nonProfileUsers.reduce((prev, current) => { if ( prev.find( user => user.user_id === current.user_id || user.email === current.email ) ) return prev return [...prev, current] }, []) const activeUsersAndInactiveUsers = [...activeUsers, ...inactiveUsers] const result = filteredNonProfileUsers.reduce((prev, current) => { if ( activeUsersAndInactiveUsers.find( user => user.user_id === current.user_id || user.email === current.email ) ) return prev return [...prev, current] }, []) console.log(result)
Use reduce()
to loop inside array and include()
to filter element match or not match:使用
reduce()
在数组内循环,使用include()
过滤元素匹配或不匹配:
var activeUsers = [ { "org_id": 1, "user_id": 9, "firstName": "Joj", "lastName": "Blonde", "email": "bop@test.ca", }, {"org_id": 1, "user_id": 1, "firstName": "Tyler", "lastName": "Whitfield", "email": "tyler@atg.net", }, ] var inactiveUsers = [ { "org_id": 1, "user_id": 3, "firstName": "James", "lastName": "Bond", "email": "test@test.ca", }, {"org_id": 1, "user_id": 2, "firstName": "Chen", "lastName": "rain", "email": "posihdun@gmail.com", } ] //user that are in different profiles var nonProfileUsers = [ { "org_id": 2, "user_id": 2, "firstName": "Chen", "lastName": "rain", "email": "posihdun@gmail.com", }, { "org_id": 3, "user_id": 6, "firstName": "weak", "lastName": "jdf", "email": "not@not.ca", }, { "org_id": 3, "user_id": 2, "firstName": "Chen", "lastName": "rain", "email": "posihdun@gmail.com", }, { "org_id": 4, "user_id": 2, "firstName": "Chen", "lastName": "rain", "email": "posihdun@gmail.com", }, { "org_id": 3, "user_id": 3, "firstName": "James", "lastName": "Bond", "email": "test@test.ca", }, { "org_id": 2, "user_id": 5, "firstName": "test", "lastName": "test", "email": "tester@tester.ca", }, { "org_id": 2, "user_id": 3, "firstName": "James", "lastName": "Bond", "email": "test@test.ca", }, ] const scaledNonProfileUsers = nonProfileUsers.reduce((prev, curr) => (prev.map(el => el.user_id).includes(curr.user_id) || prev.map(el => el.email).includes(curr.email)) ? [...prev] : [...prev, curr], []) const combinedUsers = [...inactiveUsers, ...activeUsers] const finalObj = scaledNonProfileUsers.reduce((prev, curr) => (combinedUsers.map(el => el.email).includes(curr.email)) ? [...prev] : [...prev, curr], []) console.log(finalObj)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.