将对象转换为乘法数组
Transform object to multiply array
这是我从 api
得到的对象
{
status: true,
code: 200,
msg: "Successfully",
response: {
1609927200: {
o: "1.2338",
h: "1.2344",
l: "1.2333",
c: "1.23395",
v: "5436",
t: 1609927200,
tm: "2021-01-06 10:00:00",
},
1609930800: {
o: "1.2338",
h: "1.23495",
l: "1.2333",
c: "1.2337",
v: "5333",
t: 1609930800,
tm: "2021-01-06 11:00:00",
},
1609934400: {
o: "1.23375",
h: "1.23495",
l: "1.233",
c: "1.234",
v: "5636",
t: 1609934400,
tm: "2021-01-06 12:00:00",
},
}
}
我想把它变成像这样的数组对象
{
ohlcv: [
[1609927200, 1.2338, 1.2344, 1.2333, 1.23395, 5436],
[1609930800, 1.2338, 1.23495, 1.2333, 1.2337, 5333],
[1609934400, 1.23375, 1.23495, 1.233, 1.234, 5636],
],
}
经过大量努力,我最终得到了以下代码的结果:
*this.symbol hold the object for the example*
var res = Object.keys(this.symbol).map((item) => {
return {
ohlvc: Object.keys(this.symbol[item]).map((key) => {
return Object.values(this.symbol[item][key]);
}),
};
})[3];
{
"ohlvc": [
[ "1.2338", "1.2344", "1.2333", "1.23395", "5436", 1609927200, "2021-01-06 10:00:00" ],
[ "1.2338", "1.23495", "1.2333", "1.2337", "5333", 1609930800, "2021-01-06 11:00:00" ],
[ "1.23375", "1.23495", "1.233", "1.234", "5636", 1609934400, "2021-01-06 12:00:00" ],
]
}
但是:
- 这些值应该没有引号(我已经尝试过 JSON.stringify() 但我得到了非常疯狂的结果)
2.and第5个位置的元素timestamp应该排在第一位
感谢任何帮助
您可以将所有值转换为数字。
const
object = { status: true, code: 200, msg: "Successfully", response: { 1609927200: { o: "1.2338", h: "1.2344", l: "1.2333", c: "1.23395", v: "5436", t: 1609927200, tm: "2021-01-06 10:00:00" }, 1609930800: { o: "1.2338", h: "1.23495", l: "1.2333", c: "1.2337", v: "5333", t: 1609930800, tm: "2021-01-06 11:00:00" }, 1609934400: { o: "1.23375", h: "1.23495", l: "1.233", c: "1.234", v: "5636", t: 1609934400, tm: "2021-01-06 12:00:00" } } },
keys = ['o', 'h', 'l', 'c', 'v'],
result = { ohlcv: Object
.entries(object.response)
.map(([v, o]) => [v, ...keys.map(k => o[k])].map(Number)) };
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
这是处理方法,具有 key
(按顺序排列所需的字符顺序)。
使用 Object.values
、map
和 +
运算符(转换为 int)。
const process = (obj, key = "ohlcv") => ({
[key]: Object.values(obj.response).map((item) =>
[new Date(item.tm).getTime()].concat([...key].map((kk) => item[kk]))
),
});
const data = {
status: true,
code: 200,
msg: "Successfully",
response: {
1609927200: {
o: "1.2338",
h: "1.2344",
l: "1.2333",
c: "1.23395",
v: "5436",
t: 1609927200,
tm: "2021-01-06 10:00:00",
},
1609930800: {
o: "1.2338",
h: "1.23495",
l: "1.2333",
c: "1.2337",
v: "5333",
t: 1609930800,
tm: "2021-01-06 11:00:00",
},
1609934400: {
o: "1.23375",
h: "1.23495",
l: "1.233",
c: "1.234",
v: "5636",
t: 1609934400,
tm: "2021-01-06 12:00:00",
},
},
};
console.log(process(data))
此函数将转换为您的数字的两倍,将对象转换为数组并操作它们以将时间戳显示为第一个元素。
只需用 res 变量喂它,我在下面放了一个工作示例。
const data = Object.values(res['response']).map(item => Object.values(item).map((value, index, item) => item.length === index + 1 ? value : parseFloat(value)));
const result = {
ohlcv: [...data.map(list => [
...list.filter(array => array[array.length - 1]),
...list.filter(array => !array[array.length - 1])
])]
};
如果您有任何问题,请告诉我:)
const res = {
status: true,
code: 200,
msg: "Successfully",
response: {
1609927200: {
o: "1.2338",
h: "1.2344",
l: "1.2333",
c: "1.23395",
v: "5436",
t: 1609927200,
tm: "2021-01-06 10:00:00",
},
1609930800: {
o: "1.2338",
h: "1.23495",
l: "1.2333",
c: "1.2337",
v: "5333",
t: 1609930800,
tm: "2021-01-06 11:00:00",
},
1609934400: {
o: "1.23375",
h: "1.23495",
l: "1.233",
c: "1.234",
v: "5636",
t: 1609934400,
tm: "2021-01-06 12:00:00",
},
}
};
const data = Object.values(res['response']).map(item => Object.values(item).map((value, index, item) => item.length === index + 1 ? value : parseFloat(value)));
const result = {
ohlcv: [...data.map(list => [
...list.filter(array => array[array.length - 1]),
...list.filter(array => !array[array.length - 1])
])]
};
console.log(result);
这是我从 api
得到的对象{
status: true,
code: 200,
msg: "Successfully",
response: {
1609927200: {
o: "1.2338",
h: "1.2344",
l: "1.2333",
c: "1.23395",
v: "5436",
t: 1609927200,
tm: "2021-01-06 10:00:00",
},
1609930800: {
o: "1.2338",
h: "1.23495",
l: "1.2333",
c: "1.2337",
v: "5333",
t: 1609930800,
tm: "2021-01-06 11:00:00",
},
1609934400: {
o: "1.23375",
h: "1.23495",
l: "1.233",
c: "1.234",
v: "5636",
t: 1609934400,
tm: "2021-01-06 12:00:00",
},
}
}
我想把它变成像这样的数组对象
{
ohlcv: [
[1609927200, 1.2338, 1.2344, 1.2333, 1.23395, 5436],
[1609930800, 1.2338, 1.23495, 1.2333, 1.2337, 5333],
[1609934400, 1.23375, 1.23495, 1.233, 1.234, 5636],
],
}
经过大量努力,我最终得到了以下代码的结果:
*this.symbol hold the object for the example*
var res = Object.keys(this.symbol).map((item) => {
return {
ohlvc: Object.keys(this.symbol[item]).map((key) => {
return Object.values(this.symbol[item][key]);
}),
};
})[3];
{
"ohlvc": [
[ "1.2338", "1.2344", "1.2333", "1.23395", "5436", 1609927200, "2021-01-06 10:00:00" ],
[ "1.2338", "1.23495", "1.2333", "1.2337", "5333", 1609930800, "2021-01-06 11:00:00" ],
[ "1.23375", "1.23495", "1.233", "1.234", "5636", 1609934400, "2021-01-06 12:00:00" ],
]
}
但是:
- 这些值应该没有引号(我已经尝试过 JSON.stringify() 但我得到了非常疯狂的结果)
2.and第5个位置的元素timestamp应该排在第一位
感谢任何帮助
您可以将所有值转换为数字。
const
object = { status: true, code: 200, msg: "Successfully", response: { 1609927200: { o: "1.2338", h: "1.2344", l: "1.2333", c: "1.23395", v: "5436", t: 1609927200, tm: "2021-01-06 10:00:00" }, 1609930800: { o: "1.2338", h: "1.23495", l: "1.2333", c: "1.2337", v: "5333", t: 1609930800, tm: "2021-01-06 11:00:00" }, 1609934400: { o: "1.23375", h: "1.23495", l: "1.233", c: "1.234", v: "5636", t: 1609934400, tm: "2021-01-06 12:00:00" } } },
keys = ['o', 'h', 'l', 'c', 'v'],
result = { ohlcv: Object
.entries(object.response)
.map(([v, o]) => [v, ...keys.map(k => o[k])].map(Number)) };
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
这是处理方法,具有 key
(按顺序排列所需的字符顺序)。
使用 Object.values
、map
和 +
运算符(转换为 int)。
const process = (obj, key = "ohlcv") => ({
[key]: Object.values(obj.response).map((item) =>
[new Date(item.tm).getTime()].concat([...key].map((kk) => item[kk]))
),
});
const data = {
status: true,
code: 200,
msg: "Successfully",
response: {
1609927200: {
o: "1.2338",
h: "1.2344",
l: "1.2333",
c: "1.23395",
v: "5436",
t: 1609927200,
tm: "2021-01-06 10:00:00",
},
1609930800: {
o: "1.2338",
h: "1.23495",
l: "1.2333",
c: "1.2337",
v: "5333",
t: 1609930800,
tm: "2021-01-06 11:00:00",
},
1609934400: {
o: "1.23375",
h: "1.23495",
l: "1.233",
c: "1.234",
v: "5636",
t: 1609934400,
tm: "2021-01-06 12:00:00",
},
},
};
console.log(process(data))
此函数将转换为您的数字的两倍,将对象转换为数组并操作它们以将时间戳显示为第一个元素。 只需用 res 变量喂它,我在下面放了一个工作示例。
const data = Object.values(res['response']).map(item => Object.values(item).map((value, index, item) => item.length === index + 1 ? value : parseFloat(value)));
const result = {
ohlcv: [...data.map(list => [
...list.filter(array => array[array.length - 1]),
...list.filter(array => !array[array.length - 1])
])]
};
如果您有任何问题,请告诉我:)
const res = {
status: true,
code: 200,
msg: "Successfully",
response: {
1609927200: {
o: "1.2338",
h: "1.2344",
l: "1.2333",
c: "1.23395",
v: "5436",
t: 1609927200,
tm: "2021-01-06 10:00:00",
},
1609930800: {
o: "1.2338",
h: "1.23495",
l: "1.2333",
c: "1.2337",
v: "5333",
t: 1609930800,
tm: "2021-01-06 11:00:00",
},
1609934400: {
o: "1.23375",
h: "1.23495",
l: "1.233",
c: "1.234",
v: "5636",
t: 1609934400,
tm: "2021-01-06 12:00:00",
},
}
};
const data = Object.values(res['response']).map(item => Object.values(item).map((value, index, item) => item.length === index + 1 ? value : parseFloat(value)));
const result = {
ohlcv: [...data.map(list => [
...list.filter(array => array[array.length - 1]),
...list.filter(array => !array[array.length - 1])
])]
};
console.log(result);