我目前正在尝试阅读.vdf
文件,Valve's proprietary "KeyValue" file format。我发现一个简单的VDF to JSON converter似乎很适合我的需要,但JSON输出的格式不是很理想。例如,对于以下VDF输入:
"LibraryFolders"
{
"TimeNextStatsReport" "1523464730"
"ContentStatsID" "8627456721266182211"
"1" "G:\\Steam"
}
我得到以下JSON输出:
{
"LibraryFolders": {
"1": "G:\\\\Steam",
"TimeNextStatsReport": "1523464730",
"ContentStatsID": "8627456721266182211"
}
}
我需要一种方法来抓取LibraryFolders
对象中的所有变量,并使用严格的数字名称(1,2,3 ......)并将它们放入一个数组中,去掉TimeNextStatsReport
和ContentStatsID
个字段。如何做到这一点?
我认为最容易使用的输出如下:
{
"LibraryFolders": [
"G:\\\\Steam",
...
]
}
省略号显然表示任何数字命名字段的延续。
答案 0 :(得分:1)
解析JSON后,我们可以使用以下组合:MPI_Alltoall()
,Object.entries
& Array.filter
,以达到所需的格式。
Array.map

答案 1 :(得分:0)
这里有一个示例代码可以做你想要的(我认为)
//lets take your example object.
let LibraryFolders= {
"1": "G:\\\\Steam",
"TimeNextStatsReport": "1523464730",
"ContentStatsID": "8627456721266182211",
"2": "kasjfkljaklsjf",
"1241212213": "alskdjlkasjkladsjasj",
"123jkjk123123": "aksjdljasdkljklsad",
};
//we define a function that will do the magic
function getArrayVersion(obj){
return Object.keys(obj).filter(key => {
if (!isNaN(key)) {
return key
// return parseInt(key); //in case that you need the INT conversion.
}
});
}
//we call the magic
console.log(getArrayVersion(LibraryFolders))

答案 2 :(得分:0)
我会给你一个单循环样本:
let object = {
"LibraryFolders": {
"1": "G:\\\\Steam",
"TimeNextStatsReport": "1523464730",
"ContentStatsID": "8627456721266182211",
"2": "kasjfkljaklsjf",
"1241212213": "alskdjlkasjkladsjasj",
"123jkjk123123": "aksjdljasdkljklsad",
}
};
object['LibraryFolders'] = Object.keys(object['LibraryFolders']).reduce((acc, current) => (/^\d+$/.test(current) && acc.push(object['LibraryFolders'][current]), acc), []);
console.log(object);