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

187
Visualizações
How to sort and fetch the data based on certain condition in array of objects?

I have 2 array of objects one for USA and another for Canada. The data goes in the following way

const data = [
{country: {cntryShortName:"USA"}}
{country: {cntryShortName:"USA"}}
{country: {cntryShortName:"CAN"}}
{country: {cntryShortName:"USA"}}
{country: {cntryShortName:"CAN"}}
{country: {cntryShortName:"CAN"}}
{country: {cntryShortName:"USA"}}
{country: {cntryShortName:"USA"}}
]

Here I am expecting to divide the data into 2 like as

USA: 
.....
.....

CAN:
.....
.....

This is what I've tried

const allData  = data.filter(item => item.country)

Here I'm getting all the array data but I want to sort it based on country name but hard coding isn't expected. Any optimal solution

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

0

you can do something like this

const data = [
{country: {cntryShortName:"USA"}},
{country: {cntryShortName:"USA"}},
{country: {cntryShortName:"CAN"}},
{country: {cntryShortName:"USA"}},
{country: {cntryShortName:"CAN"}},
{country: {cntryShortName:"CAN"}},
{country: {cntryShortName:"USA"}},
{country: {cntryShortName:"USA"}}
]

const order = ['USA', 'CAN']

const reorder = data.sort((a, b) => {
  return order.indexOf(a.country.cntryShortName) - order.indexOf(b.country.cntryShortName)

})

console.log(reorder)

about 4 years ago · Juan Pablo Isaza Relatório

0

I think you would most likely have to have a new object where you will push the data in after looping over the array. Something like:

let allData = {};

data.forEach(function (countryData) {
    if (allData[countryData.country.cntryShortName] !== undefined) {
        // Push the data into the array
        allData[countryData.country.cntryShortName].push(countryData);
    } else {
        // Initialize an empty array if the country key doesn't exist
        allData[countryData.country.cntryShortName] = [];
    }
});

You'll end up with something like this:

{
    "USA": [
        {
            "country": {
                "cntryShortName": "USA"
            }
        },
    ],
    "CAN": [
        {
            "country": {
                "cntryShortName": "CAN"
            }
        },
    ]
}

So now you will just be able to grab the country data using:

allData.USA
// or
allData.CAN
about 4 years ago · Juan Pablo Isaza Relatório

0

JavaScript (Node.js), 74 bytes

f=(a,g=e=>parseInt(e.country.cntryShortName,36))=>a.sort((a,b)=>g(a)-g(b))

Try it online!

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