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

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

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 Relatório

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 Relatório

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 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