J
J
just_letov2021-12-08 15:51:18
JavaScript
just_letov, 2021-12-08 15:51:18

How to properly infer and set conditions for a groovy loop?

61b0a8cdc9065092032480.jpeg

The essence of the question is that there is a certain array of data, it has a value with dates (timestep in milliseconds), you need to build conditions for the cycle that will allow you to find users who received (
something) before a certain point in time.
The output format should be
Name:
Received Date:
So that's the question, how to make such an output format and what condition to use for the loop (to find them).
I hope I phrased everything correctly)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
black1277, 2021-12-09
@just_letov

Without knowing the exact format of the input data (an array of objects or an array of arrays or something else), it is impossible to give an exact answer. Well, I will give a solution for one of the options:

const startDate = new Date("2019-01-01") // ключевая дата
const inTmStamp = startDate.getTime()
const data = [
['Anton', 1352300800000],
['Igor', 1546300800000],
['Vasiliy', 1559300809800],
['Stephan', 1546300800000],
['Jose', 1446300800000],
]
function leadZero(str){ // добавляет ноль спереди, если цифра одна
    return String(str).length<2 ? '0'+str : str
}
function formatTime(dt){
    // нумерация месяцев с 0, поэтому +1
    return dt.getFullYear() + '-' + leadZero(dt.getMonth()+1) + '-' + leadZero(dt.getDay())
}
const res = data.filter(arr => arr[1] < inTmStamp).map(arr => {
    const dt = new Date(arr[1])
    return {
        'Дата создания':  formatTime(dt),
        'Сотрудник': arr[0]
    }
})
console.log(JSON.stringify(res))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question