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

166
Visualizações
Sort function with undefined value

I have an array of object like below:

 var stores = [
      {
        "name": "store3",
        "ditance": 8
      },
      {
        "name": "Store5",
        "distance": 7,
        "web": {
          "validateAttributes": {
            "isSelectedDispostionSupported": true,
            "isFutureOrderPossible": false
          }
        }
      },
      {
        "name": "Store1",
        "distance": 12,
        "web": {
          "validateAttributes": {
            "isSelectedDispostionSupported": true,
            "isOpen": true
          }
        }
      },
      {
        "name": "store2",
        "distance": 13,
        "web": {
          "validateAttributes": {
            "isSelectedDispostionSupported": true,
            "isOpen": true
          }
        }
      }
    ]

Expected Result to be sort based on isopen and distance

[
  {
    "name": "Store1",
    "distance": 12,
    "web": {
      "validateAttributes": {
        "isSelectedDispostionSupported": true,
        "isOpen": true
      }
    }
  },
  {
    "name": "store2",
    "distance": 13,
    "web": {
      "validateAttributes": {
        "isSelectedDispostionSupported": true,
        "isOpen": true
      }
    }
  },
  {
    "name": "Store5",
    "distance": 7,
    "web": {
      "validateAttributes": {
        "isSelectedDispostionSupported": true,
        "isFutureOrderPossible": false
      }
    }
  },
  {
    "name": "store3",
    "ditance": 8
  }
]

Need to sort an array based on isOpen and distance .The challenge is some object have web property and some object don't have. Even if web available sometimes isOpen won't their. I have tried the below approch it's not working

const sorter = (a, b) => {
                if (a.web) {
                  if (a.web.validateAttributes) {
                    if (a.web.validateAttributes.isOpen) {
                      return 1;
                    } else if (b.web.validateAttributes.isOpen) {
                      return -1;
                    } else {
                      return 1;
                    };
                  } else {
                    return 1;
                  }
                 
                } else {
                  return 1;
                }
              };
  stores.sort(sorter);
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Try this

const stores = [{"name":"store3","ditance":8},{"name":"Store5","distance":7,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isFutureOrderPossible":false}}},{"name":"Store1","distance":12,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isOpen":true}}},{"name":"store2","distance":13,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isOpen":true}}}]

   const sorter = (a,b)=> {
      if(!b.web) {
          return -1;
      }
      else if(!b.web.validateAttributes) {
        return -1;
      }
      else if(!b.web.validateAttributes.isOpen) {
        return -1;
      }
      return (a.distance - b.distance)
   }
   console.log(stores.sort(sorter)) 

about 4 years ago · Juan Pablo Isaza Relatório

0

Try this.

const stores = [{"name":"store3","ditance":8},{"name":"Store5","distance":7,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isFutureOrderPossible":false}}},{"name":"Store1","distance":12,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isOpen":true}}},{"name":"store2","distance":13,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isOpen":true}}}]

const sorted = stores.slice();

sorted.sort((a, b) => {
    if (!a.web || !b.web) {
        return -1;
    }
    if (!a.web.validateAttributes || !b.web.validateAttributes) {
        return -1;
    }
    const x = a.web.validateAttributes.isOpen || false;
    const y = b.web.validateAttributes.isOpen || false;

    return (x === y) ? a.distance - b.distance : -1;
})
console.log(sorted)
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Relatório

0

I see 2 problems.

  1. typo at the first item "ditance": 8
  2. Your sorting function. Here is my logic. Firstly, check both are Opening or Closing, then compare distance. Else just simple compare the state open/close

var stores = [{"name":"store3","distance":8},{"name":"Store5","distance":7,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isFutureOrderPossible":false}}},{"name":"Store1","distance":12,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isOpen":true}}},{"name":"store2","distance":13,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isOpen":true}}}];
    
stores.sort((a, b) => {
  if (a.web?.validateAttributes?.isOpen && b.web?.validateAttributes?.isOpen || !a.web?.validateAttributes?.isOpen && !b.web?.validateAttributes?.isOpen) {
    return a.distance - b.distance;
  }
  if (a.web?.validateAttributes?.isOpen) {
    return -1;
  }
  return 1;
})

console.log(stores);

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