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

68
Vistas
Object to array stack null value at the end

I'm actually working on a vue apexchart stat module and I have to handle null value,

So I have to count the number of event in a day and I have three type of event, a, b and c which can happen same or different day, so I end up with three objects:

const ObjectA = [
{'20/10/21': 3},
{'25/10/21': 1}
],

const ObjectB = [
{'25/10/21': 4}
]

So I this case I want to have two arrays, which have to be that way:

const ArrayA = [3,1]
const ArrayB = [null, 4]

So first of all I fill up the void between the two object this way to end up with these two:

const ObjectA = [
{'20/10/21': 3},
{'25/10/21': 1}
],

const ObjectB = [
{'20/10/21': null},
{'25/10/21': 4}
]

But when I try to convert these objects to an array I always end up with the null value stacking at the end of my Array such as

const ArrayA = [3,1]
const ArrayB = [4, null]

And whatever the method I'm using, Object.values(ObjectX), a forEach loop with Object.entries(ObjectX) etc... I always have an array that does not fit my expectations.

So in a first time I'm wondering why the null values are always ended up at the end of my arrays and secondly I'm asking for an answer to see where I'm missing my point.

There is a preview to the actual code I'm using:

 const momentStartDate = moment(paramStartDate)
 const momentEndDate = moment(paramEndDate)

 if (moment(created).isBetween(momentStartDate, momentEndDate, 'days', '[]')) {
   if (!newLabels.includes(createdDate)) {
     newLabels.push(createdDate)
   }
   if (!open[createdDate]) {
     open[createdDate] = 0
   }
    open[createdDate]++
 } else if ...

newLabels.forEach((item) => {
 if (open[item] === undefined) {
  open[item] = null
 }
})

[...]

const openTemp = []

for (let i = 0; i < Object.keys(open).length; i++) {
  openTemp.push(Object.values(open)[i])
}

Object.keys(closed).forEach((item) => {
  if (closed[item] === null) {
    openTemp.push(null)
  } else {
    openTemp.push(closed[item])
   }
})

// Both do not work
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You could do it this way, breaking down each step into a function:

  • Add missing dates to each opposite object array (addMissing())
  • Sort each object array (sort())
  • Get the values of each object array (getValues)

const ObjectA = [{ "20/10/21": 3 }, { "25/10/21": 1 }];
const ObjectB = [{ "25/10/21": 4 }];

const addMissing = (oppObj) => {
  return (obj) => {
    let [[date]] = Object.entries(obj);
    if (!oppObj.some((obj) => Object.entries(obj)[0][0] === date)) {
      oppObj.push({ [date]: null });
    }
  };
};

const sort = (a, b) => {
  const splitA = Object.keys(a)[0].split("/");
  const splitB = Object.keys(b)[0].split("/");

  return (
    new Date(splitA[2], splitA[1] - 1, splitA[0]) -
    new Date(splitB[2], splitB[1] - 1, splitB[0])
  );
};

const getValues = (o) => Object.values(o)[0];

// Add missing dates to each opposite object array
ObjectA.forEach(addMissing(ObjectB));
ObjectB.forEach(addMissing(ObjectA));

// Sort each object array
ObjectA.sort(sort);
ObjectB.sort(sort);

// Get the values of each object array
const resA = ObjectA.map(getValues);
const resB = ObjectB.map(getValues);

console.log(resA);
console.log(resB);

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