I have three objects each are serialized from Django-rest framework.
Each object is a array of a user in a profile. Each profile needs one org_id and email to be valid. "activeUsers" and "inactiveUsers" will never have a
duplicates, each element shares the same org_id, and user_id and email will always be different. "nonProfileUsers" are all the other profiles that exlude
"activeUsers" and "inactiveUsers" org_id, note user_id and email can and will show up more then once.
activeUsers = [
{ "org_id": 1, "user_id": 9, "firstName": "Joj", "lastName": "Blonde", "email": "[email protected]", },
{"org_id": 1, "user_id": 1, "firstName": "Tyler", "lastName": "Whitfield", "email": "[email protected]", },
]
inactiveUsers = [
{ "org_id": 1, "user_id": 3, "firstName": "James", "lastName": "Bond", "email": "[email protected]", },
{"org_id": 1, "user_id": 2, "firstName": "Chen", "lastName": "rain", "email": "[email protected]", }
]
//user that are in different profiles
nonProfileUsers = [
{ "org_id": 2, "user_id": 2, "firstName": "Chen", "lastName": "rain", "email": "[email protected]", },
{ "org_id": 3, "user_id": 6, "firstName": "weak", "lastName": "jdf", "email": "[email protected]", },
{ "org_id": 3, "user_id": 2, "firstName": "Chen", "lastName": "rain", "email": "[email protected]", },
{ "org_id": 4, "user_id": 2, "firstName": "Chen", "lastName": "rain", "email": "[email protected]", },
{ "org_id": 3, "user_id": 3, "firstName": "James", "lastName": "Bond", "email": "[email protected]", },
{ "org_id": 2, "user_id": 5, "firstName": "test", "lastName": "test", "email": "[email protected]", },
{ "org_id": 2, "user_id": 3, "firstName": "James", "lastName": "Bond", "email": "[email protected]", },
]
I am using java-script and I need to do a few things. 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)
scaledNonProfileUsers = [
{ "org_id": 2, "user_id": 2, "firstName": "Chen", "lastName": "rain", "email": "[email protected]", },
{ "org_id": 3, "user_id": 6, "firstName": "weak", "lastName": "jdf", "email": "[email protected]", },
{ "org_id": 2, "user_id": 5, "firstName": "test", "lastName": "test", "email": "[email protected]", },
{ "org_id": 2, "user_id": 3, "firstName": "James", "lastName": "Bond", "email": "[email protected]", },
]
combined "activeUsers" and "inactiveUsers" -I was able to complete step 2
combinedUsers= [
{ "org_id": 1, "user_id": 9, "firstName": "Joj", "lastName": "Blonde", "email": "[email protected]", },
{"org_id": 1, "user_id": 1, "firstName": "Tyler", "lastName": "Whitfield", "email": "[email protected]", },
{ "org_id": 1, "user_id": 3, "firstName": "James", "lastName": "Bond", "email": "[email protected]", },
{"org_id": 1, "user_id": 2, "firstName": "Chen", "lastName": "rain", "email": "[email protected]", }
]
Make a new object from comparing "scaledNonProfileUsers" vs "combinedUsers". The emails that are in "scaledNonProfileUsers" and not in "combinedUsers" will make the final object.
finalObj= [
{ "org_id": 3, "user_id": 6, "firstName": "weak", "lastName": "jdf", "email": "[email protected]", },
{ "org_id": 2, "user_id": 5, "firstName": "test", "lastName": "test", "email": "[email protected]", },
]
I am really struggling with steps 1 and 3.
If I isolate just the email I can make a array like so
[
0:"[email protected]",
1:"[email protected]"
]
But I need to include at least one org_id and email.
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.
CodePudding user response:
Use reduce()
to loop inside array and include()
to filter element match or not match:
var activeUsers = [
{ "org_id": 1, "user_id": 9, "firstName": "Joj", "lastName": "Blonde", "email": "[email protected]", },
{"org_id": 1, "user_id": 1, "firstName": "Tyler", "lastName": "Whitfield", "email": "[email protected]", },
]
var inactiveUsers = [
{ "org_id": 1, "user_id": 3, "firstName": "James", "lastName": "Bond", "email": "[email protected]", },
{"org_id": 1, "user_id": 2, "firstName": "Chen", "lastName": "rain", "email": "[email protected]", }
]
//user that are in different profiles
var nonProfileUsers = [
{ "org_id": 2, "user_id": 2, "firstName": "Chen", "lastName": "rain", "email": "[email protected]", },
{ "org_id": 3, "user_id": 6, "firstName": "weak", "lastName": "jdf", "email": "[email protected]", },
{ "org_id": 3, "user_id": 2, "firstName": "Chen", "lastName": "rain", "email": "[email protected]", },
{ "org_id": 4, "user_id": 2, "firstName": "Chen", "lastName": "rain", "email": "[email protected]", },
{ "org_id": 3, "user_id": 3, "firstName": "James", "lastName": "Bond", "email": "[email protected]", },
{ "org_id": 2, "user_id": 5, "firstName": "test", "lastName": "test", "email": "[email protected]", },
{ "org_id": 2, "user_id": 3, "firstName": "James", "lastName": "Bond", "email": "[email protected]", },
]
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)
CodePudding user response:
const activeUsers = [
{
org_id: 1,
user_id: 9,
firstName: 'Joj',
lastName: 'Blonde',
email: '[email protected]',
},
{
org_id: 1,
user_id: 1,
firstName: 'Tyler',
lastName: 'Whitfield',
email: '[email protected]',
},
]
const inactiveUsers = [
{
org_id: 1,
user_id: 3,
firstName: 'James',
lastName: 'Bond',
email: '[email protected]',
},
{
org_id: 1,
user_id: 2,
firstName: 'Chen',
lastName: 'rain',
email: '[email protected]',
},
]
const nonProfileUsers = [
{
org_id: 2,
user_id: 2,
firstName: 'Chen',
lastName: 'rain',
email: '[email protected]',
},
{
org_id: 3,
user_id: 6,
firstName: 'weak',
lastName: 'jdf',
email: '[email protected]',
},
{
org_id: 3,
user_id: 2,
firstName: 'Chen',
lastName: 'rain',
email: '[email protected]',
},
{
org_id: 4,
user_id: 2,
firstName: 'Chen',
lastName: 'rain',
email: '[email protected]',
},
{
org_id: 3,
user_id: 3,
firstName: 'James',
lastName: 'Bond',
email: '[email protected]',
},
{
org_id: 2,
user_id: 5,
firstName: 'test',
lastName: 'test',
email: '[email protected]',
},
{
org_id: 2,
user_id: 3,
firstName: 'James',
lastName: 'Bond',
email: '[email protected]',
},
]
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)