我需要能够循环任何 JSON 文件,然后将其与任何给定的数组进行比较。例如,如果您给它一个包含的 package.json
{
"name": "testy",
"version": "0.1.0",
"description": "Testy Westy",
"main": "bin/testy",
"repository": "https://github.com/testy/westy.git",
"author": "Testy Westy",
"license": "MIT",
"private": false,
"scripts": {},
"engines": {
"node": ">= 8",
"yarn": ">= 1"
},
"dependencies": {
"chalk": "^2.3.0",
"commander": "^2.12.2",
"inquirer": "^4.0.1",
"ts-node": "^3.3.0",
"typescript": "^2.6.2",
"vorpal": "^1.12.0"
},
"devDependencies": {
"@types/commander": "^2.11.0",
"@types/inquirer": "^0.0.35",
"@types/node": "^8.5.2",
"tslint": "^5.8.0",
"tslint-config-prettier": "^1.6.0"
}
}
和一个数组:
const generalInfo = [
'name',
'version',
'description',
'repository',
'author',
'license',
'private',
]
如果给定的字段存在,它会从 JSON 中提取给定的字段,同时忽略缺失的字段,不会出现错误。能够同时保留键和值会更好。
我正在谈论的一个例子:
const pkg = require('package.json')
const generalInfo = ['name','version','description', 'engines.test']
search(pkg, generalInfo)
将返回:
D:\Testy>ts-node app/testy.ts
name: testy
version: 0.1.0
description: Testy Westy
Warning: "engines.test" does not exist.
编辑:我实际上忘记添加我已经尝试过的内容。这是我需要的最小版本。我不需要有人为我编写代码,我只需要帮助扩展我所做的事情。
function search(object: any) {
if (object instanceof Object) {
for (const key in object) {
if (object.hasOwnProperty(key)) {
search(object[key])
const element = object[key]
console.log(Chalk.default.magentaBright(`${key}:`), element)
}
}
}
}
此代码循环访问一个对象,并简单地记录键和值。我不知道如何仅根据数组提取我需要的内容。
最佳答案
您可以将一个函数传递给 JSON.parse,该函数将仅允许数组中的键。
const result = JSON.parse(pkg, (k, v) =>
!k || generalInfo.includes(k) ? v : undefined
);
const pkg = `{
"name": "testy",
"version": "0.1.0",
"description": "Testy Westy",
"main": "bin/testy",
"repository": "https://github.com/testy/westy.git",
"author": "Testy Westy",
"license": "MIT",
"private": false,
"scripts": {},
"engines": {
"node": ">= 8",
"yarn": ">= 1"
},
"dependencies": {
"chalk": "^2.3.0",
"commander": "^2.12.2",
"inquirer": "^4.0.1",
"ts-node": "^3.3.0",
"typescript": "^2.6.2",
"vorpal": "^1.12.0"
},
"devDependencies": {
"@types/commander": "^2.11.0",
"@types/inquirer": "^0.0.35",
"@types/node": "^8.5.2",
"tslint": "^5.8.0",
"tslint-config-prettier": "^1.6.0"
}
}`
const generalInfo = [
'name',
'version',
'description',
'repository',
'author',
'license',
'private',
];
const result = JSON.parse(pkg, (k, v) =>
!k || generalInfo.includes(k) ? v : undefined
);
console.log(result);
您还可以从数组中创建一个Set
以获得更好的性能。
const s = new Set(generalInfo);
const result = JSON.parse(pkg, (k, v) => !k || s.has(k) ? v : undefined);
const pkg = `{
"name": "testy",
"version": "0.1.0",
"description": "Testy Westy",
"main": "bin/testy",
"repository": "https://github.com/testy/westy.git",
"author": "Testy Westy",
"license": "MIT",
"private": false,
"scripts": {},
"engines": {
"node": ">= 8",
"yarn": ">= 1"
},
"dependencies": {
"chalk": "^2.3.0",
"commander": "^2.12.2",
"inquirer": "^4.0.1",
"ts-node": "^3.3.0",
"typescript": "^2.6.2",
"vorpal": "^1.12.0"
},
"devDependencies": {
"@types/commander": "^2.11.0",
"@types/inquirer": "^0.0.35",
"@types/node": "^8.5.2",
"tslint": "^5.8.0",
"tslint-config-prettier": "^1.6.0"
}
}`
const generalInfo = [
'name',
'version',
'description',
'repository',
'author',
'license',
'private',
];
const s = new Set(generalInfo);
const result = JSON.parse(pkg, (k, v) =>
!k || s.has(k) ? v : undefined
);
console.log(result);
关于javascript - TypeScript:循环 JSON 并匹配给定的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47964210/