Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

78
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda