Home > Mobile >  given 2 arrays , create an object, matching the element of array1, with the key of the object of arr
given 2 arrays , create an object, matching the element of array1, with the key of the object of arr

Time:05-14

I really don't know where I'm failing, I try 1000 ways, but I can't.

function userCheck (arr1,arr2) {

  let map = {};

  arr1.filter((user) => {

    arr2.filter((obj) => {


      if (user == obj.user) {
        map[user] = obj;
      } 
    
      else {
        map['untracked'] = [user];
      }


    });

  });

console.log(map)

}


userCheck([39471379, 44471379, 25471379, 35471379, 29471379,55471379],[{user: 39471379, salary: 250000}, {user: 44471379, salary: 260000}, {user: 35471379, salary: 148700},{user: 29471379, salary: 270500}]);

Create a function that receives 2 arrays, one with user numbers and the other with objects that contain data about an employee in a company (user number and salary). An object should be created where the user number is the key and the value will be the object with all the data. // In case of not finding an employee with a certain user, it means that he does not work for that company. Therefore, a new entry must be created, where the key will be “untracked” and as a value, it will have an array with all the user numbers that are not found.

expected Output:

{39471379: {ID: 39471379, salary: 250000},
44471379: {ID: 44471379, salary: 260000},
35471379: {ID: 35471379, salary: 148700},
29471379: {ID: 29471379, salary: 270500},
"untracked": [25471379,55471379]}

CodePudding user response:

You could build an object of all user data and reduce the ids of the user and get the wanted data structure by checking the object.

function userCheck(users, data) {
    const ids = Object.fromEntries(data.map(o => [o.user, o]));
    return users.reduce((r, user) => {
        if (ids[user]) r[user] = ids[user];
        else r.untracked.push(user);
        return r;
    }, { untracked: []});
}

const
    users = [39471379, 44471379, 25471379, 35471379, 29471379, 55471379],
    data = [{ user: 39471379,  salary: 250000 }, { user: 44471379, salary: 260000 }, { user: 35471379, salary: 148700 }, { user: 29471379, salary: 270500 }],
    result = userCheck(users, data);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

CodePudding user response:

Using a combination of one loop, and find.

const ids = [39471379, 44471379, 25471379, 35471379, 29471379,55471379];
const users = [{user: 39471379, salary: 250000}, {user: 44471379, salary: 260000}, {user: 35471379, salary: 148700},{user: 29471379, salary: 270500}];

function userCheck(ids, users) {
  
  // Create two output arrays, one for the users
  // that are found, the other for the untracked users
  const output = [];
  const untracked = [];
  
  // Iterate over the ids array
  for (const id of ids) {
    
    // If there is a user matching that id...
    const foundUser = users.find(user => {
      return user.user === id;
    });
    
    // ...add the user object to the output array 
    if (foundUser) {
      output.push({ [id]: foundUser });
    
    // Otherwise push the id into the untracked array
    } else {
      untracked.push(id);
    }
  }
  
  // Finally return the output array, and the
  // tracked array as one merged array
  return [...output, { untracked }];
}

console.log(userCheck(ids, users));

Additional documentation

CodePudding user response:

I have tried using a combination of forEach loop and find() method.

const ids = [39471379, 44471379, 25471379, 35471379, 29471379,55471379];
const users = [{user: 39471379, salary: 250000}, {user: 44471379, salary: 260000}, {user: 35471379, salary: 148700},{user: 29471379, salary: 270500}];
    function userCheck (ids, users) {
        let outputObj = {};
        outputObj["untracked"] = [];
        console.log(outputObj);
    
        ids.forEach(element => {
            const id = element;
            const found = users.find(ele => ele.user === element);
            if(found) {
                outputObj[id] = {
                    [id] : found
                }
            } else {
                outputObj["untracked"].push(id);
            }
        });
        return outputObj;
    
    const outputObj = userCheck(ids, users);
    console.log(outputObj);

CodePudding user response:

You can create an o object from the data array with the desired format for all users using Array.prototype.reduce() that are going to be spread within the result object using Spread syntax (...) and finally for untracked just one time Array.prototype.filter() out of users the ids that are not in the o object properties

Code:

const users = [39471379, 44471379, 25471379, 35471379, 29471379, 55471379]
const data = [{ user: 39471379, salary: 250000 },{ user: 44471379, salary: 260000 },{ user: 35471379, salary: 148700 },{ user: 29471379, salary: 270500 }]

const userCheck = (users, data) => {
  const o = data.reduce((a, c) => (a[c.user] = c, a), {})
  return {
    ...o,
    untracked: users.filter(user => !o[user])
  }
}

console.log(userCheck(users, data))
.as-console-wrapper { max-height: 100% !important; top: 0; }

  • Related