Json 将对象过滤到不同的数组中
Json Filter Objects Into Different Array
假设我有此用户数据
{
"asdfasdf": {
"user": "asdfasdf",
"skillpoints": 0,
"acc": 0,
"sessions": 0,
"wpm": 0
},
"matthewproskils": {
"user": "matthewproskils",
"skillpoints": 0,
"acc": 0,
"sessions": 0
}
}
有什么方法可以将这些数据排序成 3 个数组吗?
我希望数组的结果为 [[matthewproskils, 0],[asdfasdf, 0]]
嗯,你的例子在原来的 JSON 中似乎只有 2 个键。
但是如果你想创建一个键数组和内容数组,你可以
const jsonObject = {
"asdfasdf": {
"user": "asdfasdf",
"skillpoints": 0,
"acc": 0,
"sessions": 0,
"wpm": 0
},
"matthewproskils": {
"user": "matthewproskils",
"skillpoints": 0,
"acc": 0,
"sessions": 0
}
}
let keys= [];
let keyContents = [];
for (const [key, value] of Object.entries(jsonObject)) {
keys.push(key);
keyContents.push(value);
}
它会输出
keys = ['asdfasdf','matthewproskils']
keyContents [{
"user": "matthewproskils",
"skillpoints": 0,
"acc": 0,
"sessions": 0
},{
"user": "matthewproskils",
"skillpoints": 0,
"acc": 0,
"sessions": 0
}];
假设我有此用户数据
{
"asdfasdf": {
"user": "asdfasdf",
"skillpoints": 0,
"acc": 0,
"sessions": 0,
"wpm": 0
},
"matthewproskils": {
"user": "matthewproskils",
"skillpoints": 0,
"acc": 0,
"sessions": 0
}
}
有什么方法可以将这些数据排序成 3 个数组吗? 我希望数组的结果为 [[matthewproskils, 0],[asdfasdf, 0]]
嗯,你的例子在原来的 JSON 中似乎只有 2 个键。
但是如果你想创建一个键数组和内容数组,你可以
const jsonObject = {
"asdfasdf": {
"user": "asdfasdf",
"skillpoints": 0,
"acc": 0,
"sessions": 0,
"wpm": 0
},
"matthewproskils": {
"user": "matthewproskils",
"skillpoints": 0,
"acc": 0,
"sessions": 0
}
}
let keys= [];
let keyContents = [];
for (const [key, value] of Object.entries(jsonObject)) {
keys.push(key);
keyContents.push(value);
}
它会输出
keys = ['asdfasdf','matthewproskils']
keyContents [{
"user": "matthewproskils",
"skillpoints": 0,
"acc": 0,
"sessions": 0
},{
"user": "matthewproskils",
"skillpoints": 0,
"acc": 0,
"sessions": 0
}];