Первый подход
Я знаю, жесткие коды - ужасная идея.
let newInputForTimeSpent = 14;
const copyOfExpenses = {
...state,
expenses: {
...state.expenses,
byId: {
...state.expenses.byId,
k948zpnp: {
...state.expenses.k948zpnp,
timeSpent: state.expenses.k948zpnp.timeSpent * newInputForTimeSpent
},
z9e8ipnp: {
...state.expenses.z9e8ipnp,
timeSpent: state.expenses.z9e8ipnp.timeSpent * newInputForTimeSpent
}
},
allIds: [...state.expenses.allIds]
}
}
Второй подход
const state = {
expenses: {
byId: {
k948zpnp: {
id: 'k948zpnp',
category: 'other',
description: 'book',
amount: '25',
timeSpent: '2.5',
time: '2020-4-21 10:48:10'
},
z9e8ipnp: {
id: 'z9e8ipnp',
category: 'food',
description: 'pasta',
amount: '12',
timeSpent: '1.2',
time: '2020-4-21 11:48:10'
},
},
allIds: ['k948zpnp', 'z9e8ipnp']
}
}
const updateTimeSpentForExpenses = (state, newInputForTimeSpent) => {
let byId = { ...state.expenses.byId
}
const newObjs = Object.entries(byId).map(([key, value]) => {
return {
[key]: {
...value,
timeSpent: value.timeSpent * newInputForTimeSpent
}
}
})
for (let newObj of newObjs) {
Object.assign(byId, newObj)
}
return {
...state,
expenses: {
...state.expenses,
byId,
allIds: [...state.expenses.allIds]
}
}
}
console.log(updateTimeSpentForExpenses(state, 10))
Вместо for of loop
с Object.assign
можно использовать Array.reduce
const newObjs = Object.entries(byId).map(([key, value]) => {
return {
[key]: {
...value,
timeSpent: value.timeSpent * newInputForTimeSpent
}
}
})
for(let newObj of newObjs) {
Object.assign(byId, newObj)
}
Становится
byId = Object.entries(byId).map(([key, value]) => {
return [key, {
...value,
timeSpent: value.timeSpent * newInputForTimeSpent
}]
}
}).reduce((acc, ([key, value]) => {
acc[key] = value;
return acc;
}, {})