gpt4 book ai didi

javascript - 在转换日期时从数组到带有服装键的对象

转载 作者:行者123 更新时间:2023-11-30 11:20:40 24 4
gpt4 key购买 nike

我正在尝试将这种数组转换为对象,同时格式化日期以便能够将其与图表一起使用。

这是我到目前为止所做的一个片段

arr=[1524314460000,0.067872,0.067876,0.067876,0.06785,0.41500986]
var obj = {...arr}
const newKeys = {0: new Date("time"), 1: "open" , 2:"low",3:"high",4:"close",5:"volume" };
function renameKeys(obj, newKeys) {
const keyValues = Object.keys(obj).map(key => {
const newKey = newKeys[key] || key;
return { [newKey]: obj[key] };
});
return Object.assign({}, ...keyValues);
}
const renamedObj = renameKeys(obj, newKeys);
console.log(renamedObj)

如您所见,我能够将其转换为一个对象,但我不能使用 new Date() 来转换时间,如您所见,关于如何处理任务的任何想法我得到无效日期

谢谢

最佳答案

newKeys 中删除 new Date(),因为您需要在 time 键的值中添加日期。只需在 map 中添加一个条件,即可检查该属性是否为 time。如果是,请使用 new Date(obj[key])

将其更改为日期

arr=[1524314460000,0.067872,0.067876,0.067876,0.06785,0.41500986]
var obj = {...arr}
const newKeys = {0: "time", 1: "open" , 2:"low",3:"high",4:"close",5:"volume" };
function renameKeys(obj, newKeys) {
const keyValues = Object.keys(obj).map(key => {
if(newKeys[key] === 'time'){
obj[key] = new Date(obj[key]);
}
const newKey = newKeys[key] || key;
return { [newKey]: obj[key] };
});
return Object.assign({}, ...keyValues);
}
const renamedObj = renameKeys(obj, newKeys);
console.log(renamedObj)

关于javascript - 在转换日期时从数组到带有服装键的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49955987/

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