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

93
Views
Sort/move array data into three different arrays

I've got an array with data which I would like sort/move to three different arrays. The data in the array looks like this: data_array["101","08:00","45","102","08:00","46","102","08:00","47","101","08:01","46","102","08:01","45"] ... The first data is an id, the second a time stamp and the third a temperature and then it start over. How can I do this?

This is my attempt:

    var id = []
    var time_stamp = []
    var temperature = []

    data_array = ["101","08:00","45","102","08:00","46","102","08:00","47","101","08:01","46","102","08:01","45"]
    
    int counter = 1;
    foreach(var item in data_array)
    {
         if(counter == 1)
         {
         vlan_id.push(item);
         counter++;
         }
         else if (counter == 2)
         {
         time_stamp.push(item);
         counter++;
         }
         else if (counter == 3)
         {
         temperature.push(item);
         counter = 1;
         }
     }
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You could take an index and increment for each part.

const
    id = [],
    time_stamp = [],
    temperature = [],
    data = ["101", "08:00", "45", "102", "08:00", "46", "102", "08:00", "47", "101", "08:01", "46", "102", "08:01", "45"];
    
let i = 0;

while (i < data.length) {
    id.push(data[i++]);
    time_stamp.push(data[i++]);
    temperature.push(data[i++]);
}

console.log(...id);
console.log(...time_stamp);
console.log(...temperature);

A slightly different approach

const
    id = [],
    time_stamp = [],
    temperature = [],
    data = ["101", "08:00", "45", "102", "08:00", "46", "102", "08:00", "47", "101", "08:01", "46", "102", "08:01", "45"],
    targets = [id, time_stamp, temperature];
    
let i = 0;

while (i < data.length) {
    targets[i % targets.length].push(data[i++]);
}

console.log(...id);
console.log(...time_stamp);
console.log(...temperature);

about 4 years ago · Juan Pablo Isaza Report

0

Pretty much what you're doing is correct.

var id = []
var time_stamp = []
var temperature = []

const data_array = ["101","08:00","45","102","08:00","46","102","08:00","47","101","08:01","46","102","08:01","45"]
   
let counter = 1;

for(const d of data_array) {
    if(counter === 1) {
  id.push(d);
  } else if(counter === 2) {
  time_stamp.push(d);
  } else if(counter === 3) {
  temperature.push(d);
  }
  counter++;
  if(counter > 3) counter = 1;
}
about 4 years ago · Juan Pablo Isaza Report

0

A potential solution using .reduce().

Code Snippet

const data_array = ["101", "08:00", "45", "102", "08:00", "46", "102", "08:00", "47", "101", "08:01", "46", "102", "08:01", "45"];

const {id, time_stamp, temperature} = data_array.reduce(
  (acc, itm, idx) => ({     // iterate over array using ".reduce()"
    ...acc,
    ...(     // based on "idx" add "itm" to "id", "time_stamp" or "temperature" arrays
      idx % 3 === 0
      ? { id: acc.id.concat([itm]) }
      : idx % 3 === 1
        ? { time_stamp: acc.time_stamp.concat([itm])}
        : { temperature: acc.temperature.concat([itm])}
        
    )
  }),     // initialize 'acc' with below object
  {id: [], time_stamp: [], temperature: []}
);

console.log(
  'id: ', ...id,
  '\ntime_stamp: ', ...time_stamp,
  '\ntemperature: ', ...temperature
);

Explanation

Inline comments describe the significant aspects of the solution.

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!