Home > Net >  Sort/move array data into three different arrays
Sort/move array data into three different arrays

Time:04-07

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

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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.

CodePudding user response:

A pretty simple solution:

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"];
    
while(data.length > 0) {
  id.push(data.shift());
  time_stamp.push(data.shift());
  temperature.push(data.shift());
}

Array.shift() takes removes the first element of an array and returns it. No need to use indexes.

  • Related