如何将未标记的数组排列成标记的数组?
How to arrange an unlabeled array into a labelled array?
我正在为印度股票市场制作交易机器人。
我有这种形式的库存数据
data = [[ 1627875900, 434.75, 435.8, 432.55, 434.9, 1750806 ], [ 1627876800, 434.75, 435.2, 432.7, 433, 905388 ], [ 1627877700, 432.9, 433.75, 431.8, 433.55, 689338 ],...........]
按顺序表示为
[[time stamp, Open Price, High Price, Low Price, Close Price, Volume], [.....]]
我想将其排列在带标签的数组中
{ open: [...], close: [...], high: [...], low: [...], volume: [...] }
我正在使用 TALIB 库,它使用这种形式的数据来使用指标。
我已经看到使用 Python 和 pandas 可以完成此操作,但需要 javascript.
中的解决方案
谢谢
这是一个数据操作的例子。
const data = [[ 1627875900, 434.75, 435.8, 432.55, 434.9, 1750806 ], [ 1627876800, 434.75, 435.2, 432.7, 433, 905388 ], [ 1627877700, 432.9, 433.75, 431.8, 433.55, 689338 ]];
const open = [], close = [], high = [], low = [], volume = [];
data.forEach(elm => {
open.push(elm[1])
high.push(elm[2])
low.push(elm[3])
close.push(elm[4])
volume.push(elm[5])
});
const result = {open, high, low, close, volume};
console.log(result);
试试这个:
const data = [
[1627875900, 434.75, 435.8, 432.55, 434.9, 1750806],
[1627876800, 434.75, 435.2, 432.7, 433, 905388],
[1627877700, 432.9, 433.75, 431.8, 433.55, 689338]
]
const result = {
open: [],
high: [],
low: [],
close: [],
volume: []
}
data.forEach(item => {
result.open.push(item[1])
result.high.push(item[2])
result.low.push(item[3])
result.close.push(item[4])
result.volume.push(item[5])
})
console.log(result)
您可以使用 zip 函数按索引压缩嵌套数组,并将结果传递给预期数据形状的构造函数。
const zip = (...r) => [...r[0]].map((_, c) => r.map(s => s[c]));
const Data = ([_timestamp, open, close, high, low, volume]) => ({ open, close, high, low, volume });
// usage
const data = [[1627875900, 434.75, 435.8, 432.55, 434.9, 1750806], [1627876800, 434.75, 435.2, 432.7, 433, 905388], [1627877700, 432.9, 433.75, 431.8, 433.55, 689338]];
const result = Data(zip(...data));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
这很容易构建在一些可重用的辅助函数之上。 transpose
翻转矩阵,使列变成行。 zipObj
采用键数组和值数组,并生成一个对象,该对象具有与每个键关联的相应值。有了这些,我们几乎可以简单地写出这个:
const transpose = (xs) =>
xs [0] .map ((_, i) => xs .map (r => r[i]))
const zipObj = (keys) => (vals) =>
Object .fromEntries (transpose ([keys, vals]))
const restructure = (data) =>
zipObj (['timestamp', 'open', 'high', 'low', 'close', 'volume']) (transpose (data))
const data = [[1627875900, 434.75, 435.8, 432.55, 434.9, 1750806], [1627876800, 434.75, 435.2, 432.7, 433, 905388], [1627877700, 432.9, 433.75, 431.8, 433.55, 689338]];
console .log (restructure (data))
.as-console-wrapper {max-height: 100% !important; top: 0}
这包括 timestamp
个值。如果你不想要它们,那么我们可以用另一个助手删除每个(tail
只是 returns 一个没有第一个值的数组)然后简单地不在键列表中包含 timestamp
.您可以通过展开以下代码段来查看:
const tail = (xs) => xs .slice (1)
const transpose = (xs) =>
xs [0] .map ((_, i) => xs .map (r => r[i]))
const zipObj = (keys) => (vals) =>
Object .fromEntries (transpose ([keys, vals]))
const restructure = (data) =>
zipObj (['open', 'high', 'low', 'close', 'volume']) (transpose (data .map (tail)))
const data = [[1627875900, 434.75, 435.8, 432.55, 434.9, 1750806], [1627876800, 434.75, 435.2, 432.7, 433, 905388], [1627877700, 432.9, 433.75, 431.8, 433.55, 689338]];
console .log (restructure (data))
.as-console-wrapper {max-height: 100% !important; top: 0}
我正在为印度股票市场制作交易机器人。 我有这种形式的库存数据
data = [[ 1627875900, 434.75, 435.8, 432.55, 434.9, 1750806 ], [ 1627876800, 434.75, 435.2, 432.7, 433, 905388 ], [ 1627877700, 432.9, 433.75, 431.8, 433.55, 689338 ],...........]
按顺序表示为
[[time stamp, Open Price, High Price, Low Price, Close Price, Volume], [.....]]
我想将其排列在带标签的数组中
{ open: [...], close: [...], high: [...], low: [...], volume: [...] }
我正在使用 TALIB 库,它使用这种形式的数据来使用指标。
我已经看到使用 Python 和 pandas 可以完成此操作,但需要 javascript.
中的解决方案谢谢
这是一个数据操作的例子。
const data = [[ 1627875900, 434.75, 435.8, 432.55, 434.9, 1750806 ], [ 1627876800, 434.75, 435.2, 432.7, 433, 905388 ], [ 1627877700, 432.9, 433.75, 431.8, 433.55, 689338 ]];
const open = [], close = [], high = [], low = [], volume = [];
data.forEach(elm => {
open.push(elm[1])
high.push(elm[2])
low.push(elm[3])
close.push(elm[4])
volume.push(elm[5])
});
const result = {open, high, low, close, volume};
console.log(result);
试试这个:
const data = [
[1627875900, 434.75, 435.8, 432.55, 434.9, 1750806],
[1627876800, 434.75, 435.2, 432.7, 433, 905388],
[1627877700, 432.9, 433.75, 431.8, 433.55, 689338]
]
const result = {
open: [],
high: [],
low: [],
close: [],
volume: []
}
data.forEach(item => {
result.open.push(item[1])
result.high.push(item[2])
result.low.push(item[3])
result.close.push(item[4])
result.volume.push(item[5])
})
console.log(result)
您可以使用 zip 函数按索引压缩嵌套数组,并将结果传递给预期数据形状的构造函数。
const zip = (...r) => [...r[0]].map((_, c) => r.map(s => s[c]));
const Data = ([_timestamp, open, close, high, low, volume]) => ({ open, close, high, low, volume });
// usage
const data = [[1627875900, 434.75, 435.8, 432.55, 434.9, 1750806], [1627876800, 434.75, 435.2, 432.7, 433, 905388], [1627877700, 432.9, 433.75, 431.8, 433.55, 689338]];
const result = Data(zip(...data));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
这很容易构建在一些可重用的辅助函数之上。 transpose
翻转矩阵,使列变成行。 zipObj
采用键数组和值数组,并生成一个对象,该对象具有与每个键关联的相应值。有了这些,我们几乎可以简单地写出这个:
const transpose = (xs) =>
xs [0] .map ((_, i) => xs .map (r => r[i]))
const zipObj = (keys) => (vals) =>
Object .fromEntries (transpose ([keys, vals]))
const restructure = (data) =>
zipObj (['timestamp', 'open', 'high', 'low', 'close', 'volume']) (transpose (data))
const data = [[1627875900, 434.75, 435.8, 432.55, 434.9, 1750806], [1627876800, 434.75, 435.2, 432.7, 433, 905388], [1627877700, 432.9, 433.75, 431.8, 433.55, 689338]];
console .log (restructure (data))
.as-console-wrapper {max-height: 100% !important; top: 0}
这包括 timestamp
个值。如果你不想要它们,那么我们可以用另一个助手删除每个(tail
只是 returns 一个没有第一个值的数组)然后简单地不在键列表中包含 timestamp
.您可以通过展开以下代码段来查看:
const tail = (xs) => xs .slice (1)
const transpose = (xs) =>
xs [0] .map ((_, i) => xs .map (r => r[i]))
const zipObj = (keys) => (vals) =>
Object .fromEntries (transpose ([keys, vals]))
const restructure = (data) =>
zipObj (['open', 'high', 'low', 'close', 'volume']) (transpose (data .map (tail)))
const data = [[1627875900, 434.75, 435.8, 432.55, 434.9, 1750806], [1627876800, 434.75, 435.2, 432.7, 433, 905388], [1627877700, 432.9, 433.75, 431.8, 433.55, 689338]];
console .log (restructure (data))
.as-console-wrapper {max-height: 100% !important; top: 0}