Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

209
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda