gpt4 book ai didi

javascript - 转换对象数组中的值

转载 作者:行者123 更新时间:2023-11-28 14:52:32 25 4
gpt4 key购买 nike

我有一个对象数组 String我想要转换为 Date 的值,但我收到以下错误:

Uncaught SyntaxError: Unexpected token :

我应该如何正确地进行此转换并返回相同的对象结构?

array = [
{
end: "2017-05-18T09:00:00.000Z",
start: "2017-05-18T06:00:00.000Z"
},
{
end: "2017-05-19T07:00:00.000Z",
start: "2017-05-19T06:00:00.000Z"
},
{
end: "2017-05-20T08:00:00.000Z",
start: "2017-05-20T07:00:00.000Z"
}
]

result = array.map((element) => {
{
end: new Date(element.end),
start: new Date(element.start),
}
})

最佳答案

你要么这样做:

result = array.map(element => ({
end: new Date(element.end),
start: new Date(element.start),
}));

或者:

result = array.map(element => {
return {
end: new Date(element.end),
start: new Date(element.start),
};
})

可以看看ECMAScript6 arrow function that returns an object

关于javascript - 转换对象数组中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44009547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com