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

165
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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