Merge objects in JavaScript if conditions are met - javascript

I need to join different objects depending on their conditions.
Context
The theme is to calculate the hours that a user accumulates during a full week, let's call it "tracking".
I have an array where I store the daily history of the hours that a user performs daily represented by 'data_daily'
The following array stores the current weekly information of the day that is being consulted, for example, 2022-10-28 is consulted, so its week is from 2022-10-24 to 2022-10-30, which is represented by 'data_week'
However, there are different cases of important uses to highlight:
--- If a user does not record hours on a certain day, that means there will be no record of his hours, right? It's obvious, but to know the reason why he didn't meet his hours, it is stored in 'time_off_request_query' where the reason why he didn't track is specified and if he has an excuse or not give him a minimum of hours or by default zero
So to better understand this let's see it in an example
The user with ID 1234 did not mark hours on October 30 and when consulting the daily information for that day it does not come to us, but we have to store in 'time_off_request_query' that it was missing due to an authorized permission, and a minimum number of hours will be assigned per agreement
Time Off Request
[{
user_id: 1234,
reason: 'authorized permission',
input_date: 2022-10-31,
departure_date: 2022-10-29,
min_tracked: 4
}]
Then, when consulting the weekly information, we obtain that in the week from 2022-10-24 to 2022-10-30, specifically during the days that he did attend, he did 40 hours of work plus the minimum 4 hours assigned to him by agreement for the day 2022 -10-29 gives a total of 44 hours
Weekly Information
[{
user_id: 1234,
weklyConsulting: '2022-10-24 / 2022-10-30',
tracking Weekly: 44
}]
And when consulting the daily information of 2022-10-29 we obtain the minimum hours that were assigned by agreement
[{
user_id: 1234,
date: '2022-10-30',
trackingDaily: 4
}]
So when joining or merging the objects, a result like the following is expected:
[{
user_id: 1234,
date: '2022-10-30',
trackingDaily: 4,
weklyConsulting: '2022-10-24 / 2022-10-30',
tracking Weekly: 44
}]
What are the validations then to take into account
If a user did not do hours on the day consulted but has accumulated for the week, the minimum hours added by agreement must be added to the weekly record, as must also be represented in the Daily Information of the day consulted
If a user made hours that day consulted but during the week he asked for permission and was assigned minimum hours by agreement, he must bring what he did that day plus his accumulated hours of the week where the total will be taken into account what was get from time_off_request_query
Problem
This is my current code
export const generateReporteDiarioTesteo = async (req: Request & any, res: Response, next: NextFunction) => {
try {
const dayliHoursxTimeWork: DayliHoursxTimeWork[] = [];
const dataDailyxResume: DayliHoursxResume[] = [];
const dataWeekxResume: ResumeWeek[] = [];
const hubstaffService = new HubstaffClass();
const supabaseService = new SupabaseService();
const configGenerals = new Configs();
let data_daily: DailyActivityJoin[] = [];
let data_week: DailyDB[] = [];
let generalConfigs: GeneralConfig[] = [];
let time_holiday_request: TimeHoliday[] = [];
let attendance_schedule_request: AttendanceSchedule[] = [];
let time_off_request_query: TimeOffRequest[] = [];
const promises = [
hubstaffService.getDailyActivities(req.query.start_date, req.query.end_date, req).then(dataFromHubstaff => {
let summedTrackedHubstaff = trackedDataFromApi(dataFromHubstaff);
data_daily = summedTrackedHubstaff;
}),
supabaseService.getSummaryWeek(req.query.start_date).then(dataFromDB => {
let summedTrackedWeekly = trackedData(dataFromDB as any[]);
data_week = summedTrackedWeekly;
}),
configGenerals.getConfigs().then(dataFromDB => { generalConfigs = dataFromDB as any }),
configGenerals.get_time_holiday(req.query.start_date).then(dataFromDB => { time_holiday_request = dataFromDB as any }),
configGenerals.attendance_schedule(req.query.fecha_inicio).then(dataFromDB => { attendance_schedule_request = dataFromDB as any }),
/** LIST OF USERS WHO DID NOT TRACK THAT DAY REQUESTED */
configGenerals.time_off_request(req.query.start_date).then(dataFromDB => { time_off_request_query = dataFromDB as any }),
];
await Promise.all(promises);
generalConfigs.map(config => {
dayliHoursxTimeWork.push({
dayliHourFullTime: config.fulltime_hours_daily,
dayliHourPartTime: config.parttime_hours_daily
})
});
/** WEEKLY DATA - REGISTRY OF USERS IN THE WEEK */
data_week.map(weekData => {
dataWeekxResume.push({
user_id: weekData.user_id,
tracked_weekly: weekData.tracked,
overall_weekly: weekData.overall,
keyboard_weekly: weekData.keyboard,
input_tracked_weekly: weekData.input_tracked,
mouse_Weekly: weekData.mouse,
billable_Weekly: weekData.billable,
manual_Weekly: weekData.manual,
idle_Weekly: weekData.idle,
resumed_weekly: weekData.resumed,
rolTittle: weekData.rol.tittle,
rolePriority: weekData.rol.priority,
status: weekData.status,
username: weekData.username,
daily_hours: weekData.daily_hours,
summaryStarts: weekData.summaryStarts,
time_work: weekData.user.time_work
});
});
/** DAILY DATA */
data_daily.map(dayli => {
dataDailyxResume.push({
user_id: dayli.user_id,
report_date: dayli.date,
tracked_daily: dayli.tracked,
input_tracked_daily: dayli.input_tracked,
overall_daily: dayli.overall,
mouse_daily:dayli.mouse,
billable_daily: dayli.billable,
manual_daily: dayli.manual,
idle_daily: dayli.idle,
resumed_daily: dayli.resumed
});
});
// WEEKLY UNION X DAILY
const joinWeekDailyTracked = dataDailyxResume.map(dayliData => {
const weekData = dataWeekxResume.find(item => item.user_id === dayliData.user_id);
let dataDailyOff = getTimeOff(time_off_request_query, dayliData, weekData, req.query.start_date);
let dayliWeekOff = getTimeOffWeek(time_off_request_query, dayliData, weekData, req.query.start_date);
let accWeekOff = getAccTimeOffWeek(time_off_request_query, dayliData, weekData, req.query.start_date)
return (weekData && dayliData) ? { ...dayliData, ...weekData } : (weekData && !dayliData) ? { ...weekData, ...dataDailyOff } : (!weekData && !dayliData) ? { ...dataDailyOff, dayliWeekOff } : (!dayliData && (weekData && time_off_request_query.length > 0)) ? {...dataDailyOff, ...accWeekOff} : null
});
res.json(joinWeekDailyTracked) }
catch (error) {
res.status(500).json({ title: 'API-CIT Error', message: 'Error Interno del Servidor' });
}
}
Carry out the following functions with the idea of ​​being able to validate each use case that I exposed in the context
export function getTimeOff(time_off_request_query, dayliData, weekData, date) {
let tracked_off: number = 0;
let user_id_off: number = 0;
let username_off: string = '';
let rol_off: string = '';
let role_priority_off: string = '';
let activity_off: string = '';
// of the time_off_request_query in the week query for each element of the array
time_off_request_query.forEach(off_time => {
// if the id is different from the one that brings the daily information
if (off_time.user_id != dayliData.user_id) {
// stores the minimum number of hours assigned by agreement + extra information
tracked_off = off_time.tracked;
user_id_off = off_time.user_id;
username_off = off_time.user.name;
rol_off = off_time.rol.tittle;
rol_priority_off = off_time.rol.priority;
activity_off = off_time.activity
}
})
// Return the new object
let dataDailyOff = {
user_id: user_id_off,
report_date: date,
tracked_daily:tracked_off,
input_tracked_daily: tracked_off,
overall_daily: 0,
mouse_daily: 0,
billable_daily: 0,
manual_daily: 0,
idle_daily: 0,
resumed_daily: 0,
username: username_off
};
return dataDailyOff;
}
export function getTimeOffWeek(time_off_request_query, dayliData, weekData, date) {
let tracked_off: number = 0;
let user_id_off: number = 0;
let username_off: string = '';
let rol_off: string = '';
let role_priority_off: string = '';
let activity_off: string = '';
let role_status = true;
let dayli_hours_off: number = 0;
let time_work_off: number = 0;
// of the time_off_request_query in the week query for each element of the array
time_off_request_query.forEach(off_time => {
// If the id of the user in daily information is different from the time_off and the id of the weekly information is also different
if (off_time.user_id != dayliData.user_id && off_time.user_id != weekData.user_id) {
tracked_off = off_time.tracked;
user_id_off = off_time.user_id;
username_off = off_time.user.name;
rol_off = off_time.rol.tittle;
rol_priority_off = off_time.rol.priority;
activity_off = off_time.activity;
role_status = off_time.user.status
time_work_off = off_time.user.time_work
}
})
let dayliWeekOff = {
user_id: user_id_off,
tracked_weekly:tracked_off,
overall_weekly:tracked_off,
keyboard_weekly: 0,
input_tracked_weekly: 0,
mouse_weekly: 0,
billable_Weekly: 0,
manual_weekly: 0,
idle_Weekly: 0,
summarized_weekly: 0,
rolTitle: rol_off,
rolePriority: role_priority_off,
status: role_status,
username: username_off,
daily_hours: daily_hours_off,
summaryStarts: 0,
time_work: time_work_off
};
return dayliWeekOff;
}
export function getAccTimeOffWeek(time_off_request_query, dayliData, weekData, date) {
let tracked_off: number = 0;
let user_id_off: number = 0;
let username_off: string = '';
let rol_off: string = '';
let role_priority_off: string = '';
let activity_off: string = '';
let role_status = true;
let dayli_hours_off: number = 0;
let time_work_off: number = 0;
// of the time_off_request_query in the week query for each element of the array
time_off_request_query.forEach(off_time => {
// If the user id in daily information is equal to the time_off and the id of the weekly information is also equal to the time_off
if (off_time.user_id != dayliData.user_id && off_time.user_id === weekData.user_id) {
// fetch the information you found about the time_off of that user_id
tracked_off = off_time.tracked;
user_id_off = off_time.user_id;
username_off = off_time.user.name;
rol_off = off_time.rol.tittle;
rol_priority_off = off_time.rol.priority;
activity_off = off_time.activity;
role_status = off_time.user.status
time_work_off = off_time.user.time_work
}
})
let dayliWeekOff = {
user_id: weekData.user_id,
// And add it when returning the object
tracked_weekly: tracked_off + weekData.tracked_weekly,
overall_weekly: tracked_off + weekData.tracked_weekly,
keyboard_weekly: weekData.keyboard_weekly,
input_tracked_weekly: weekData.input_tracked_weekly,
mouse_Weekly: weekData.mouse_Weekly,
billable_Weekly: weekData.billable_Weekly,
manual_Weekly: weekData.manual_Weekly,
idle_Weekly: weekData.idle_Weekly,
resumed_weekly: weekData.resumed_weekly,
rolTittle: weekData.rolTittle,
rolPrpriority: weekData.rolPriority,
status: weekData.status,
username: weekData.username,
daily_hours: weekData.daily_hours,
summaryStarts: weekData.summaryStarts,
time_work: weekData.user.time_work
};
return dayliWeekOff;
}
In the code that I present to you, I used functions that were too extensive and even too difficult to read and understand, in addition to different bad practices that I would like to be able to do in another way.
Currently this code is not working for me, the functions that I am trying to implement according to my logic are not correct
How can I do this? Is there a way to do it with Javascript? Or better to look elsewhere?

Related

Is there a good way to loop through APIs?

There are too many loops in this API, which is causing it to run slowly.
this data is only for year i have to run same code for Day month week looping same code makes the code slower.
// ```GRAPH CALCULATION FOR YEAR```\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
let query = '';
graph_data = '';
let startOfYear = MOMENT().startOf('year');
let monthsForYear = [];
let year = [];
// Create an array of dates representing the start of each month in the year
for (let index = 0; index <= 11; index++) {
const add1Month = MOMENT(startOfYear)
.add(index, 'month')
.format('YYYY-MM-DDTHH:mm:SS.000+00:00');
monthsForYear.push(add1Month);
}
// Get the actual amount for each month
for (let i = 0; i < monthsForYear.length; i++) {
let j = i + 1;
let d = await PRISMA.orders.findMany({
where: {
created_at: {
gte: monthsForYear[i],
lte:
i === monthsForYear.length - 1 ? endOftheYear_date : monthsForYear[j],
},
},
select: { actual_amount: true },
});
// Calculate the total actual amount for the month
let total = 0;
d.forEach((el) => {
total += el.actual_amount;
});
year.push(total);
}
// Set the graph data to the calculated amounts
graphDataOfYear = year;
You execute asynchronous statements (using await) in a loop, which means they are executed consecutively: The second statement starts only after the first has completed. It may be faster if you execute them in parallel.
The first loop starts all statements in parallel, then all results are awaited together and the total actual amounts calculated in a second loop:
let statements = [];
for (let i = 0; i < monthsForYear.length; i++) {
let j = i + 1;
statements.push(PRISMA.orders.findMany({
where: {
created_at: {
gte: monthsForYear[i],
lte:
i === monthsForYear.length - 1 ? endOftheYear_date : monthsForYear[j],
},
},
select: { actual_amount: true },
}));
}
for (const d of await Promise.all(statements)) {
// Calculate the total actual amount for the month
let total = 0;
d.forEach((el) => {
total += el.actual_amount;
});
year.push(total);
}
The general pattern is:
let statements = [];
for (...) {
statements.push(/* asynchronous statement without await */);
}
for (const d of await Promise.all(statements)) {
/* d is the result of each asynchronous statement in turn */
}
It is very likely even faster if you can let PRISMA do the total actual amount calculation, because then only one number (the total actual amount) must be sent back to your Javascript code, whereas with your current code every order that exists in PRISMA is sent back for one of the months, and then again for one of the weeks, and for one of the days. But I am no PRISMA expert.

How to handle k6 Trend value where 2 values get pushed at same timestamp

I have a scenario in K6 where I get 2 values at the same timestamp from datadog. The first value is an incorrect spike. The second one is the correct value which I need.
I use K6 Trend to collect the values from Datadog. It collects it as 2 different values due to which my max value comes incorrectly as one of those spikes. How to collect it such that when the timestamp is same the latest value gets extracted?
Current implementation:
index.js:
//thresholds and scenarios get loaded in K6
export const getPaymentIdRPS = new Trend("get_payment_id_rps");
export function setup() {
//setup data
}
//this is the scenario with the issue
export function collectGetPaymentIdMetrics(data){
metrics.getPaymentIdRPS= payments.queryGetPaymentIdRPS();
metrics.getPaymentIdRPS.points.forEach(val => {
getPaymentIdRPS.add(val[1], tags);
});
}
In different library, payments.js:
function queryGetPaymentIdRPS() {
const queries = setQueryEnv();
const response = datadog.metricsQuery(queries.getPaymentIdRPS, 10); //(query, duration of extraction)
return parseMetricQuery(response); //these functions defined towards end of this Q.
}
The above returns:
INFO[0032] rps val= [1666798390000,54.5] source=console
INFO[0037] rps val= [1666798390000,15.571428571428573] source=console
INFO[0042] rps val= [1666798400000,19] source=console
INFO[0047] rps val= [1666798400000,5.428571428571429]
Hence max = 54.5 but I want max as 15.57 by storing [1666798390000,15.571428571428573] and [1666798400000,5.428571428571429] only.
metricsQuery:
metricsQuery(query, seconds) {
//Delay the query start/end timestamps to allow datadog to process metrics;
const DATADOG_INGESTION_DELAY = 30;
let start = Math.ceil(Date.now() / 1000) - seconds;
let end = Math.ceil(Date.now() / 1000);
start = start - DATADOG_INGESTION_DELAY;
end = end - DATADOG_INGESTION_DELAY;
//null body in 2nd param
let res = this.queryClient.get(
`/query?from=${start}&to=${end}&query=${query}`,
null,
{ tags: { name: `Datadog /metric` } }
);
check(res, {
"DD metric query OK": (r) => r.status === 200,
}) || fail(JSON.stringify(res));
check(res, {
"DD metric query valid": (r) => r.json().status != "error",
}) || fail(JSON.stringify(res.json().error));
return res;
}
};
parseMetricQuery:
export function parseMetricQuery(response) {
const responseBody = response.json();
//Check invalid response
if (responseBody.series === undefined || response.status != 200) {
fail("Failed to get Datadog metric");
}
//Check 0 response, map will fail if array is empty
if (responseBody.series.length === 0) {
return {
sum: 0,
length: 0,
average: 0,
points: [],
url: response.request.url,
};
} else {
const length = responseBody.series[0].length;
var series = responseBody.series[0].pointlist;
var sum = 0;
sum = series
.map((val) => {
return val[1];
})
.reduce((previousValue, currentValue) => previousValue + currentValue, 0);
return {
sum: sum,
length: length,
average: sum / length,
points: series,
url: response.request.url,
};
}
}
Since each iteration of K6 is like a fresh start, we can't have any global map whose values remain intact to compare timestamps already recorded. Hence I can't compare whether timestamp is already recorded and hence replace value of that timestamp rather than append to Trend list.

Is there a way to select only certain fields from Firestore?

I am working on a performance issue of a function, that takes 15sec to response, which makes a request to firebase for all documents that are
"ErrorID" "==" "0"
The problem is that there are many documents and they are kind of very large objects, and I only need TWO FIELDS (Order and Amount) of each document, there are any way to request only those two fields that accomplish the condition?
Something like :
firestore.collection("purchases").where("ErrorID", "==", "0").get(Order, Amount);
The function that im talking about:
const totalEarn = async (req, res, next) => {
const DAY = 86400000;
const WEEK = 604800016;
const MONTH = 2629800000;
try {
let snap = await firestore.collection("purchases").where("ErrorID", "==", "0").get(); // CONDITION
let totalToday = 0;
let totalYesterday = 0;
let totalLastWeek = 0;
let totalLastMonth = 0;
let now = Date.now();
let Yesterday = now - 86400000;
await snap.forEach((doc) => { // THIS FOR EACH TAKES TOO MUCH TIME
let info = doc.data();
let time = info.Order.split("-")[2]; // FIRESTORE FIELD -> ORDER
let amount = info.AmountEur * 1; // FIRESTORE FIELD -> AMOUNT
if (time > now - DAY) {
totalToday = totalToday + amount;
}
if (time < now - DAY && time > Yesterday - DAY) {
totalYesterday = totalYesterday + amount;
}
if (time > now - WEEK) {
totalLastWeek = totalLastWeek + amount;
}
if (time > now - MONTH) {
totalLastMonth = totalLastMonth + amount;
}
});
res.send({
status: true,
data: {
totalToday: totalToday.toFixed(2),
totalYesterday: totalYesterday.toFixed(2),
totalLastWeek: totalLastWeek.toFixed(2),
totalLastMonth: totalLastMonth.toFixed(2),
},
});
} catch (error) {
res.status(410).send({
status: false,
error: "some error occured counting the numbers",
e: error.message,
});
}
};
The document im talking about
If you use Firestore Node.JS client or Firebase Admin SDK, then you can use select() to select fields:
import { Firestore } from "#google-cloud/firestore";
const firestore = new Firestore();
const snap = firestore
.collection("purchases")
.where("ErrorID", "==", "0")
.select("Order", "Amount")
.get();

How do I search a Discord user input for a value and assign it to a var?

I'm trying to search Discord User input for integers attached to attributes (Number of attacks, skill of attack, strength, toughness, save) and then run those args through some calculations.
At the moment the function uses the order of args in the command:
!odds 5 3 4 5 3
which works, but I want something like this:
!odds at: 5 sk: 3 st: 4 t: 5 sv: 3
And the bot will search the user input for values attached to those attributes in any order and use those integers for its calculations.
In addition, if attributes aren't present the bot will not include them in the output (or assign default values) and by adding additional, single phrase attributes with no attached integer (for example fnp or res) it would then add another standard calculation and output.
This is what I have so far:
const { Message } = require('discord.js')
module.exports = {
name: 'odds',
aliases: ['stats', 'probability'],
usage: '[#attacks (1 = auto hit)] [skill] [strength] [toughness] [save (7 = no save)]',
description: 'What are the odds?',
execute(message, args) {
let attack = args[0]
let skill;
if (args[1]>=7) skill = 7;
else skill = args[1];
let strength = args[2]
let toughness = args[3]
let save;
if (args[4]<=6) save = args[4];
else save = 7;
let hits = parseFloat((attack*((7-skill)/6)).toFixed(2))
let hitSixes = parseFloat((attack*(1/6)).toFixed(2))
let woundFactor;
if (strength>=(2*toughness)) woundFactor = 2;
else
if (strength>toughness) woundFactor = 3;
else
if (strength==toughness) woundFactor = 4;
else
if (toughness>=(2*strength)) woundFactor = 6;
else
if (strength<toughness) woundFactor = 5;
let wounds = parseFloat((hits*((7-woundFactor)/6)).toFixed(2))
let woundSixes = parseFloat((hits*(1/6)).toFixed(2))
let unsavedWounds = parseFloat(((wounds-(wounds*((7-save)/6))).toFixed(2)))
message.channel.send(`On average you will make:\n${hits} successful hits (with ${hitSixes} sixes)\n${wounds} successful wounds (with ${woundSixes} sixes)\nGiving a total of ${unsavedWounds} unsaved wounds.`)
}}
Any and all help greatly appreciated!
// what you accept:
const inputs = {
at: 0,
sk: 0,
st: 0,
t: 0,
sv: 0,
}
for (let i = 0, arg; arg = args[i]; i++) {
const index = arg.indexOf(':'); // it must have an ":" as last character
if (index === -1) {
continue;
}
const keyword = arg.substr(0, index - 1); // get the word
if (!inputs.hasOwnProperty(keyword)) { // check if it's an element of inputs
continue;
}
if (i + 1 === args.length) { // make sure enough args are available
continue;
}
inputs[keyword] = isNaN(args[i + 1]) ? 0 : +args[i + 1]; // make sure it's a number
i++; // skip the next element
}
let attack = inputs.at;
let skill = inputs.sk >= 7 ? 7 : inputs.sk;
let strength = inputs.st;
let toughness = inputs.t;
let save = inputs.sv <= 6 ? inputs.sv : 7;

How to properly type a .map loop argument

So I have a timezone list and has a type of the following:
type TimezoneListType = {
label: string;
name: string;
offset: number;
lower?: string;
offsetString?: string;
};
const TIMEZONE_LIST: TimezoneListType[] = [
{
label: 'Niue',
name: 'Pacific/Niue',
offset: -11,
},
{
label: 'Pago Pago',
name: 'Pacific/Pago_Pago',
offset: -11,
},
//... and so on
];
const TIMEZONE_NAME_MAP: any = {};
const TIMEZONE_MAP = TIMEZONE_LIST.map((item) => {
const positive = item.offset >= 0;
const hour = item.offset | 0;
const minute = (item.offset - hour) * 60;
return TIMEZONE_NAME_MAP[item.name] = { // typescript is screaming in this line. if try to change the `any` to `TimezoneListType`
...item,
lower: item.label.toLowerCase(),
offsetString: 'something',
};
});
on the lower end of the code you'll see that I'm transforming timezone list to have keys of item.name then adding some property lower and offsetString.
My problem is item.name is giving me:
Element implicitly has an 'any' type because index expression is not of type number ts(7015)
on both item and name when I hover at them. And I'm not sure how to type it correctly.
EDIT: Ideal result of the transformation is the photo below:
I've just put any on the map argument like TIMEZONE_LIST.map((item: any)
Update: I've changed my object declaration type to
const TIMEZONE_NAME_MAP: Record<string, TimezoneListType> = {};
seems to work but I don't understand why? and Now the other part of my program is screaming as I've exported it to other places so I can use it like TIMEZONE_NAME_MAP.filter( ..so on) but does't recognize indexOf as I'm trying to use that inside the filter function. what do i do?
The assignment on the last line implies a for loop may be better suited to this scenario than a map. The purpose of the map is to create an array mapped to new values corresponding to the original, whereas your goal here is to create a dictionary.
PS, you also don’t seem to be using the values that you create during the mapping anywhere after assigning them (positive, minute)
.map operator returns one single object, no need to assign
const TIMEZONE_NAME_MAP: TimezoneListType[] = TIMEZONE_LIST.map((item) => {
const positive = item.offset >= 0;
const hour = item.offset | 0;
const minute = (item.offset - hour) * 60;
return {
...item,
lower: item.label.toLowerCase(),
offsetString: 'something',
};
});
Your getting that error because you are assigning an object to the array index and it is expecting you to an numeric index.
const TIMEZONE_MAP = TIMEZONE_LIST.map((item) => {
const positive = item.offset >= 0;
const hour = item.offset | 0;
const minute = (item.offset - hour) * 60;
return TIMEZONE_NAME_MAP[index] = { // typescript is screaming in this line.
...item,
lower: item.label.toLowerCase(),
offsetString: 'something',
};
});
EDIT: this would be a better way:
const TIMEZONE_MAP = TIMEZONE_LIST.map((item, index) => {
const positive = item.offset >= 0;
const hour = item.offset | 0;
const minute = (item.offset - hour) * 60;
return { // typescript is screaming in this line.
...item,
lower: item.label.toLowerCase(),
offsetString: 'something',
};
});
Edit 2 as per new requirement:
const TIMEZONE_MAP = TIMEZONE_LIST.map((item, index) => {
const positive = item.offset >= 0;
const hour = item.offset | 0;
const minute = (item.offset - hour) * 60;
let obj = {};
obj[item.name] = {...item,
lower: item.label.toLowerCase(),
offsetString: 'something'};
return obj;
});

Categories

Resources