Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Trying to make a new object from three exsisting objects using javascript

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": "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.
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": "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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 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". 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": "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.

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.

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.

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading