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

152
Visualizações
Generate new order of numbers from array

I'm currently trying to generate a new order of numbers from a current array with numbers from 1 - 10.

For example, I have arrary like this:

data = [
    {
        0: 1,
        1: 2,
        2: 3,
        3: 4,
        4: 5,
        5: 6
    },
    {
        0: 1,
        1: 2,
        2: 3,
        3: 4,
        4: 5,
        5: 8
    }
]

And i'm trying to generate a new order from this array from numbers from 1 - 10.

For example: I want it to generate a new order like this:

    0: 1,
    1: 2,
    2: 3,
    3: 4,
    4: 5,
    5: 7 (The new number)

Where it checks the order of the array, and make a new order with numbers from 1 - 10

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

First thing to do is to create an object with the total values. We can accomplish this by looping over each object in the array and add each value to a new object. For this we can use the reduce method on the data array.

After that loop over the object with the totals and divide each value with the amount of objects that are present in the data array.

Use Math.round() to round each value to the nearest whole number.

const data = [{
    0: 1,
    1: 2,
    2: 3,
    3: 4,
    4: 5,
    5: 6
  },
  {
    0: 1,
    1: 2,
    2: 3,
    3: 4,
    4: 5,
    5: 8
  }
];

function getDataTotal(data) {
  const totalObject = data.reduce((total, curObj) => {
    for (const key of Object.keys(curObj)) {
      total[key] = total[key] 
        ? total[key] + curObj[key] 
        : curObj[key];
    }

    return total;
  }, {});

  const averageObject = {};
  for (const [key, value] of Object.entries(totalObject)) {
    averageObject[key] = Math.round(value / data.length);
  }

  return averageObject;
}

const averageObject = getDataTotal(data);
console.log(averageObject);

about 4 years ago · Juan Pablo Isaza Relatório

0

Make sure you have the same keys on both objects.

const data = [
  {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6},
  {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 8},
]

getAverageValueOfMappingElements() {
      const keys = Object.keys(data[0])

      return [...keys].map(key => {
        return (data[0][key] + data[1][key]) / 2
      })
    }
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