我正在尝试转换以下对象:
let obj = {startDate: '2015-08-03', startTime: '12:00AM'}
至
let obj = {startDate: '2015-08-03 12:00AM'}
在 javascript 中执行此操作的最佳方法是什么?
更新
这就是我目前拥有的
export default object => {
let newObj = {};
Object.keys(object).forEach(item => {
if (object[item] === '') {
delete newObj[item];
} else if (moment.isMoment(object[item])) {
newObj[item] = object[item].format('YYYY-DD-MM');
} else if (item === 'startTime') {
newObj['startDate'] = `${newObj['startDate']} ${object['startTime']}`;
} else {
newObj[item] = object[item];
}
});
return newObj; };
但问题是,当我先选择时间字段,然后选择日期时,它将呈现为
startDate: "undefined 3:00AM"
我现在缺少什么?
感谢迄今为止提供的所有帮助。
最佳答案
如果你可以使用 ES6 试试这个:
let obj = {startDate: '2015-08-03', startTime: '12:00AM'}
obj = {startDate: `${obj.startDate} ${obj.startTime}`}
关于javascript - 在 javascript 中更新对象并将属性组合在一起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47370517/