Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!