javascript - TypeScript:循环 JSON 并匹配给定的数组

标签 javascript arrays json typescript

我需要能够循环任何 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/

相关文章:

c# - ASP.NET C# - 一种实时通知方法

javascript - 为什么我会收到 400 错误? Node 、Express、ReactJS

arrays - 减少格式化单元格所需的代码行数?

c# - 读取 HttpResponseMessage.Content 在读取 webapi 2 token 时抛出 Newtonsoft.Json.JsonReaderException

javascript - 强制 json_encode 在平面数组中创建数值字符串

Javascript - CreateStore - 将多个函数仅作为一个参数传递 - Redux - React Native

javascript - 如何阻止li标签在添加内容后下台?

php - 将大量数组数据插入DB

python - 是什么导致 numpy.nanstd() 返回 nan?

json - 如何将 Laravel 5.4 与 Angular 4 集成