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

174
Vistas
I have list in the array .,how do I show one of its element should come first based on condition in mapping?

Here is the object.

 var arr = {
    firstValue:"xyz",
    content:[
      {
        "value": "abc",
        "checked": true
      },
      {
        "value": "xyz",
        "checked": false
      },
      {
        "value": "lmn",
        "checked": true
      }
    ]
    }

In this firstValue is xyz so while mapping xyz should come first like this:

 var arr = {
firstValue:"xyz",
content:[
  {
    "value": "xyz",
    "checked": true
  },
  {
    "value": "abc",
    "checked": false
  },
  {
    "value": "lmn",
    "checked": true
  }
]
}

How to achieve this by using javascript,

Thanks.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

const arr = {
  firstValue: "xyz",
  content: [
    {
      value: "abc",
      checked: true,
    },
    {
      value: "xyz",
      checked: false,
    },
    {
      value: "lmn",
      checked: true,
    },
  ],
};
const index = arr.content.findIndex((item) => item.value === arr.firstValue);
if (index !== -1 && index !== 0) {
  arr.content.unshift(...arr.content.splice(index, 1));
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

It looks like you just want to sort the matching object to the top.

You can either use Array#sort(), though this will mutate the original object, so if it's in state you'll want to clone before sorting.

var arr = { firstValue: 'xyz', content: [ { value: 'abc', checked: true, }, { value: 'xyz', checked: false, }, { value: 'lmn', checked: true, }, ], };

const sorted = [...arr.content].sort((a, b) => 
  (b.value === arr.firstValue) - (a.value === arr.firstValue));

console.log(sorted);

Otherwise you can find the index of the object using Array#findIndex() and then use Array#splice() and Array#unshift() to move it to the beginning of the array. Here declaring a utility function and returning a copy of the passed array to avoid mutation.

var arr = { firstValue: 'xyz', content: [ { value: 'abc', checked: true, }, { value: 'xyz', checked: false, }, { value: 'lmn', checked: true, }, ], };

const orderFirst = (a) => {
  const res = [...a];
  const i = a.findIndex((o) => o.value === arr.firstValue);
  
  if (i && i !== -1) {
    res.unshift(res.splice(i, 1));
  }

  return res;
};

const sorted = orderFirst(arr.content);

console.log(sorted);

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