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

237
Vistas
How to get the three key/value pairs with the lowest value from an object?

I have an object as below:

var countryobj = {
    "Canada": 10,
    "Peru": 1,
    "Argentina": 5,
    "Colombia": 2,
    "Mexico": 8
};

I want to get the first 3 smallest key value pair so that my output would be:

Peru: 1
Colombia: 2
Argentina: 5
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Just get the Object.entries and sort them based on their value in ascending order, then print the first three items formatted how you like.

var countryobj = {
 "Canada": 10,
 "Peru": 1,
 "Argentina": 5,
 "Colombia": 2,
 "Mexico": 8
};

const entries = Object.entries(countryobj).sort(([, a], [, b]) => a - b);

for (let i = 0; i < 3; i++) {
  console.log(entries[i][0] + ": " + entries[i][1]);
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

function getRange(obj, count) {
  return Object.entries(obj)
    .sort(([, prev], [, next]) => a - b)
    .slice(0, count)
    .reduce(
      (result, [key]) => ({
        ...result,
        [key]: obj[key],
      }),
    {},
  );
}
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