-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?
Excuse me, can you suggest a change?
– programadornoobie
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.
– Augusto Vasques
Okay, thanks for your help!!! I will implement these improvements in the question
– programadornoobie
The time you put the [MCVE] you earn a +1.
– Augusto Vasques
Improved???????
– programadornoobie
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.
– Augusto Vasques
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?
– programadornoobie
I’m leaving for lunch after lunch I can even give a suggestion.
– Augusto Vasques
All right, thank you, Augusto
– programadornoobie