Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

210
Vistas
given 2 arrays , create an object, matching the element of array1, with the key of the object of array2

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]}
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

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; }

about 4 years ago · Juan Pablo Isaza Denunciar

0

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

  • Spread syntax
about 4 years ago · Juan Pablo Isaza Denunciar

0

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);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda