我正在尝试将这种数组转换为对象,同时形成日期以便能够将其与图表一起使用。
这是我到目前为止所做的一段摘录
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()
来转换时间,任何关于如何处理任务的想法都可以看到我得到invalid date
。
谢谢
答案 0 :(得分:1)
从new Date()
移除newKeys
,因为您需要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)

答案 1 :(得分:1)
您也可以使用Array
或reduce()
直接使用map
转换为对象,以获得所需的结果。
<强>样本强>
const arr = [1524314460000, 0.067872, 0.067876, 0.067876, 0.06785, 0.41500986],
newKeys = {
0: 'time',
1: "open",
2: "low",
3: "high",
4: "close",
5: "volume"
};
function renameKeys(arr, newKeys) {
return arr.reduce((r,v,i)=>{
i = newKeys[i];
r[i]= i == 'time'? new Date(v):v;
return r;
},{});
}
console.log(renameKeys(arr, newKeys))
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0;}
&#13;
答案 2 :(得分:1)
似乎只是使用数组索引就足够了:
Error:(22, 19) java: illegal character: '\u00bc'
Error:(22, 17) java: not a statement
Error:(63, 24) java: illegal character: '\u00bc'
Error:(63, 25) java: invalid method declaration; return type required
&#13;