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

301
Vistas
how to sort arrays while ignoring null values in java script
initialArray = [ obj1, obj2, obj3, obj4, obj5, obj6, obj7, obj8 ]; // all eight are uniq objects
filterdArray = [ obj2, obj5, obj6, obj8 ]; // varies from 0 to 8

varOrder = [ 6, 8, 3, 2, 1, 4, 5, 7 ]

I have a varOrder as input, which is from 1 to 8. It is always 1-8 but in different order as per user wish.

Initially the Array have Obj1 to Obj8, but after filter, the Array may or may not have elements, If it has elements, it varies from 1 to 8.

what am I looking for is if

filterdArray = [ obj2, obj5, obj6, obj8 ]; // and my varOrder is
varOrder = [ 6, 8, 3, 2, 1, 4, 5, 7 ]; // required ouput is like below

FinalObjects = [ obj6, obj8, obj2, obj5 ]

In simple, out of 8 in the order give by user, if some objects are not available skip to next object.

The Reasoning for FinalObjects = [ obj6, obj8, obj2, obj5 ] is

varOrder | FilteredArray

6 | obj6 available from FilterdArray (obj6)
8 | obj8 available from FilteredArray (obj8)
3 | obj3 is not available from FilteredArray
2 | obj2 is available from FilteredArray (obj2)
1 | obj1 is not available from FilteredArray
4 | obj4 is not available from FilteredArray
5 | obj5 is available from FilteredArray (obj5)
7 | obj7 is not availabe from FilteredArray

Thus FinalObjects = [ obj6, obj8, obj2, obj5 ]

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

0

Perform the mapping first, then filter.

const varOrder = [ 6, 8, 3, 2, 1, 4, 5, 7 ];
const initialArray = [ obj1, obj2, obj3, obj4, obj5, obj6, obj7, obj8 ];

const orderedArray = varOrder.map((idx) => initialArray[idx - 1]);
// [ obj6, obj8, obj3, obj2, obj1, obj4, obj5, obj7 ]

const filteredArray = orderedArray.filter(obj => obj !== null);
// Assume all objects are null except for 6, 8, 2, and 5.
// [ obj6, obj8, obj2, obj5 ]
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use this approach:

First, we map each element of filterdArray to an order index, since this is an expensive operation, and we do not want to run it every time we rearrange the array.

Then we sort the mapped array by the order index.

Finally, we return the original objects.

const initialArray = [{n:'obj1'}, {n:'obj2'}, {n:'obj3'}, {n:'obj4'}, {n:'obj5'}, {n:'obj6'}, {n:'obj7'}, {n:'obj8'}];
const filterdArray = [{n:'obj2'}, {n:'obj5'}, {n:'obj6'}, {n:'obj8'}]; 
const order = [ 6, 8, 3, 2, 1, 4, 5, 7 ];

const mapped = filterdArray.map((obj) => (
  { obj, order: order.at(initialArray.findIndex((item)=> item.n === obj.n)) }));

console.log('mapped >>', mapped);

const sortedByOrder = mapped
  .sort((o1, o2) => o1.order - o2.order)
  .map(({obj}) => obj);
  
console.log('sortedByOrder >>', sortedByOrder);
.as-console-wrapper {max-height: 100% !important; top: 0}

about 4 years ago · Juan Pablo Isaza Denunciar

0

Hope this is the expected result. just used a for loop

let obj1 = {name: 'obj1'};let obj2 = {name: 'obj2'};let obj3 = {name: 'obj3'};let obj4 = {name: 'obj4'};let obj5 = {name: 'obj5'};let obj6 = {name: 'obj6'};let obj7 = {name: 'obj7'};let obj8 = {name: 'obj8'}

initialArray = [ obj1, obj2, obj3, obj4, obj5, obj6, obj7, obj8 ]; // all eight are uniq objects
filterdArray = [ obj2, obj5, obj6, obj8 ]; // varies from 0 to 8

varOrder1 = [ 6, 3, 8, 2, 1, 4, 5, 7 ];
varOrder2 = [ 7, 5, 4, 1, 2, 8, 3, 6 ];

let finalArr1 = [];
let finalArr2 = [];

const sorter = (varOrder) => {
    let finalArr = []
  for (let i = 0; i < varOrder.length; i++) {
  
    if (filterdArray.includes(initialArray[varOrder[i]-1])){
        finalArr.push(initialArray[varOrder[i]-1])
    }
   }
   return finalArr;
}       

console.log(sorter(varOrder1))

console.log(sorter(varOrder2))
.as-console-wrapper { max-height: 100% !important; top: 0; }

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