感谢任何帮助!我总是upvote btw
所以我有这个成员名单
export const currentMembers= {
'A1': {
'memberStartDate': 0,
'currentContractEndDate': 0,
'flagged': false,
'notes': 'Some Notes about member',
'memberId': 'A1',
},
'A2': {
'memberStartDate': 1454306400000,
'currentContractEndDate': 1501747600000,
'notes': 'Some Notes about member',
'flagged': false,
'memberId': 'A2',
},
'A3': {
'memberStartDate': 1454306400000,
'currentContractEndDate': 1502747600000,
'notes': 'Some Notes about member',
'flagged': false,
'memberId': 'A3',
},
}
我试图编写一个函数,它会返回相同的列表,但插入的计算值如下:
export const currentMembersModified = {
'A1': {
'memberStartDate': 0,
'currentContractEndDate': 0,
'daysRemainingInContract': 0, // calculated from currentMembers object above
'flagged': false,
'notes': 'Some Notes about member',
'memberId': 'A1',
},
'A2': {
'memberStartDate': 1454306400000,
'currentContractEndDate': 1501747600000,
'daysRemainingInContract': 123, // calculated from currentMembers object above
'notes': 'Some Notes about member',
'flagged': false,
'memberId': 'A2',
},
'A3': {
'memberStartDate': 1454306400000,
'currentContractEndDate': 1502747600000,
'daysRemainingInContract': 234, // calculated from currentMembers object above
'notes': 'Some Notes about member',
'flagged': false,
'memberId': 'A3',
},
}
我一直在尝试:
const modifiedMembers = Object.keys(currentMembers).forEach(key => {
const currentMemberObject = currentMembers[key]
const currentDate = new Date()
const currentEndDate = currentMembers[key].currentContractEndDate
const calculatedDateStuff = currentEndDate - currentDate
return {stuff I don't know how to do}
})
谢谢!
答案 0 :(得分:1)
这是一个解决方案,但如果您想保留currentMembers
的原始副本,请使用Object.assign()
或ES6点差运算符制作副本。
for (key in currentMembers) {
var currentMemberObject = currentMembers[key]
var currentDate = new Date()
var currentEndDate = currentMembers[key].currentContractEndDate
var calculatedDateStuff = currentEndDate - currentDate;
// fix logic to calculate number of days yourself, however, here is how
// you need to update those objects
currentMembers[key]['daysRemainingInContract'] = calculatedDateStuff
}
console.log(currentMembers);
答案 1 :(得分:1)
您可以@Norris在How do I get the number of days between two dates in JavaScript? enter image description here使用for..of
循环,Object.entries()
,Object.assign()
方法
const currentMembersModified = new Object;
const currentMembers= {
'A1': {
'memberStartDate': 0,
'currentContractEndDate': 0,
'flagged': false,
'notes': 'Some Notes about member',
'memberId': 'A1',
},
'A2': {
'memberStartDate': 1454306400000,
'currentContractEndDate': 1501747600000,
'notes': 'Some Notes about member',
'flagged': false,
'memberId': 'A2',
},
'A3': {
'memberStartDate': 1454306400000,
'currentContractEndDate': 1502747600000,
'notes': 'Some Notes about member',
'flagged': false,
'memberId': 'A3',
}
}
for (const [key, prop] of Object.entries(currentMembers)) {
const {memberStartDate, currentContractEndDate} = prop;
const [to, from] = [
// https://stackoverflow.com/a/20082660/
Math.floor(new Date(currentContractEndDate).getTime() / (3600*24*1000)),
Math.floor(new Date(memberStartDate).getTime() / (3600*24*1000))
];
Object.assign(currentMembersModified
, {[key]:Object.assign({}, prop, {daysRemainingInContract: to - from})});
}
console.log(currentMembersModified);
答案 2 :(得分:1)
如果你正在使用babel和你的反应项目,那么根据你所包含的阶段,你可能会尝试这样的事情:
var newMembers; // new object/state
Object.keys(currentMembers).forEach((key) => {
var calculatedDays = currentMembers[key].currentContractEndDate - 21343214213; // or whatever logic you need here
var member = {};
member[key] = {
...currentMembers[key],
calculatedDays
}
newMembers = {
...newMembers,
...member
}
});
这使用spread运算符组合对象。 ES6目前只允许使用扩展运算符组合迭代,但由于babel将它们与对象一起使用是一种常见的React模式,因为这意味着您可以避免更改原始对象。