Last active
February 1, 2023 19:13
-
-
Save abel-masila/6676534fa176e7ce857c44eaec4ba98e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Value types can be three, value1, value2, value3 | |
const items=[ | |
{ | |
"date": "2023-02-01", | |
"value1": 1, | |
"value2": 1 | |
}, | |
{ | |
"date": "2023-02-03", | |
"value1": 1, | |
"value2": 1 | |
}, | |
{ | |
"date": "2023-02-02", | |
"value1": 1, | |
"value2": 1 | |
}, | |
{ | |
"date": "2023-02-07", | |
"value1": 1, | |
"value2": 1, | |
"value3": 1 | |
}, | |
{ | |
"date": "2023-02-08", | |
"value3": 1 | |
}, | |
] | |
//Notice the first 3 objects have consecutive dates and same values. | |
//I want to be able to display the consecutive dates which have same value type and value amount like this: 2023-02-01-2023-02-03: 2 | |
//The none consectuve dates remain as they are |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@abel-masila Check on how you can format it
`let start = 0;
let end = 0;
for (let i = 0; i < items.length; i++) {
if (i > 0 && items[i].value === items[i - 1].value) {
end = i;
} else {
if (end > start) {
console.log(
Dates: ${items[start].date} - ${items[end].date}, Value: ${items[start].value}
);}
start = i;
end = i;
}
}
if (end > start) {
console.log(
Dates: ${items[start].date} - ${items[end].date}, Value: ${items[start].value}
);}`