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

231
Visualizações
Combine two arrays on primary key and foreign key and sort data

I have two arrays like this:

let employees = [
    {
        "id":"1",
        "name" : "John Doe"
    },
    {
        "id":"4",
        "name" : "Peter Jones"
    },
    {
        "id":"3",
        "name" : "Jack Johnson"
    },
    {
        "id":"2",
        "name" : "Ron Morris"
    }
]

let salaries = [
    {
        "employeeId" : "1",
        "salary": "1500"
    },
    {
        "employeeId" : "2",
        "salary": "150"
    },
    {
        "employeeId" : "3",
        "salary": null 
    },
    {
        "employeeId" : "4",
        "salary": "1780"
    }
]

Result should be an exposed function for retrieving employees with their salaries from the server.

Function should support returning employees ascending or descending by salary.

I tried something like this but it does not work:

let result = employees.map(e => ({...e, salary: salaries.filter(({ employeeId }) => employeeId === e.id).sort((a,b) => (a.salary > b.salary) ? 1 : ((b.salary > a.salary) ? -1 : 0))}));

Thanks in advance.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This addresses the broader requirements of making it a function, allowing for a parameter that determined ascending or descending order, and also where to place null values.

let employees = [
    {
        "id": "1",
        "name": "John Doe"
    },
    {
        "id": "4",
        "name": "Peter Jones"
    },
    {
        "id": "3",
        "name": "Jack Johnson"
    },
    {
        "id": "2",
        "name": "Ron Morris"
    }
];
let salaries = [
    {
        "employeeId": "1",
        "salary": "1500"
    },
    {
        "employeeId": "2",
        "salary": "150"
    },
    {
        "employeeId": "3",
        "salary": null
    },
    {
        "employeeId": "4",
        "salary": "1780"
    }
];

/**
 * @param {boolean} true for ascending, false for descending
 * @param {number} value to use for nulls to determine where to place them
 */
function combineAndSort(employees, salaries, ascending, nullAs = Number.MAX_VALUE) {
    return employees.map((employee) => {
        return {
            ...employee,
            salary: parseInt(salaries.find((salary) => salary.employeeId === employee.id)?.salary)
        };
    }).sort((a, b) => {
        const aVal = isNaN(a.salary) ? nullAs : a.salary;
        const bVal = isNaN(b.salary) ? nullAs : b.salary;
        const diff = aVal - bVal;
        return ascending ? diff : -diff;
    });
}
console.log(combineAndSort(employees, salaries, true));

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