How to group data by time interval?

Asked

Viewed 88 times

-3

I need to gather received values between a certain time interval, with javscript.

I have an Object:

var object = [
    { horario: "09:03", status: "OK"},
    { horario: "09:04", status: "OK"},
    { horario: "09:05", status: "NOK" },
    { horario: "09:06", status: "OK"},
    { horario: "09:07", status: "NOK"},
    { horario: "09:07", status: "OK"},
    { horario: "09:08", status: "NOK"},
];

And I want a return that looks like this:

retorno: [{
    status: ["OK", "OK", "NOK"],
    horario: ["09:05"]
}, {
    status: ["OK", "OK", "NOK", "NOK"]
    horario: ["09:10"]
}]

What I’ve tried to do so far is this:

//Declaração do objeto, que também poderia ser um array
var object = [
    { horario: "09:03", status: "OK"},
    { horario: "09:04", status: "OK"},
    { horario: "09:05", status: "NOK" },
    { horario: "09:06", status: "OK"},
    { horario: "09:07", status: "NOK"},
    { horario: "09:07", status: "OK"},
    { horario: "09:08", status: "NOK"},
]; 

//Declaração de contadores, e arrays que receberão os dados
var x = [];
var y = [];
var a = 0;
var b = 0;

//Laço para percorrer o objeto
for (var i = 0; i < status.length; i++) {
                    
    if (status[i] == "OK") {
        a++;
        b = b+0;
        x.push(x[i] = a);
        y.push(y[i] = b);
    } else if (status[i] == "NOK") {
        a = a+0;
        b++;
        x.push(x[i] = a);
        y.push(y[i] = b);
    }
}

It even works, but not as it should. What can I do to solve this problem?

  • 1

    Excuse me, can you suggest a change?

  • How to organize data by status. It would also be good to put a [MCVE] showing its attempt to solve the problem, I’m not saying that is your case, but many people try to use us to solve homework "effortlessly" and usually code that comes unaccompanied from trying to resolve the negative community.

  • 1

    Okay, thanks for your help!!! I will implement these improvements in the question

  • The time you put the [MCVE] you earn a +1.

  • Improved???????

  • 1

    No! Went outside the scope and threw the answer out of scope. It’s just an example minimum than how you tried to solve the same problem you presented. If you want to ask about other content open a new question.

  • I get it. I think I’m going to open up another question, and improve this one at the same time. Any suggestions for the title of the other question?

  • 1

    I’m leaving for lunch after lunch I can even give a suggestion.

  • 1

    All right, thank you, Augusto

Show 4 more comments

1 answer

-1

You can use the function filter (this way will return only when the comparison is True) or forEach (traverse the Array one time and you do the validation inside it)

var object = [
    { horario: "09:03", status: "OK"},
    { horario: "09:04", status: "OK"},
    { horario: "09:05", status: "NOK" },
    { horario: "09:06", status: "OK"},
    { horario: "09:07", status: "NOK"},
    { horario: "09:07", status: "OK"},
    { horario: "09:08", status: "NOK"},
];
//Filter
var filterOk = object.filter(j => j.status == "OK")
var filterNOk = object.filter(j => j.status == "NOK")

//ForEach Desta forma percorre somente uma vez o array
var filterOk = []
var filterNOk = []
object.forEach( ar => {
  if (ar.status =='OK') { 
    filterOk.push(ar.horario) 
  } else {
    filterNOk.push(ar.horario)
  }})


var retorno = {"OK": filterOk, "NOK": filterNOk}
  • I put the link to the filter method documentation. If you think the change misrepresents the answer you can reverse clicking on the review.

  • Thanks for your help, buddy!

  • 2

    The map is not ideal for "just for iterating" iterations. The map in theory should be used to map an array in another. In that case I think it would be more worthwhile to use a forEach or a looping loop like for, nay? :)

  • 1

    Complementing the above comment, use map only to iterate is not a good one because it returns another array (which in your case is being ignored) - it is a different array than the ones you have already created (filterOk and filterNok), with the result of callback - but like the callback does not return anything, the result will be a lot of undefined - see. If you just want to iterate through the array, in fact it would be best to use one for simple (or else the forEach - https://answall.com/a/426824/112052). Although "working", map is not the most appropriate in this case

  • I changed my answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.