TypeScript:循环访问JSON并匹配给定的数组

时间:2017-12-24 21:43:49

标签: 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)
      }
    }
  }
}

此代码遍历对象,只记录键和值。我不知道如何只根据数组拉出我需要的东西。

2 个答案:

答案 0 :(得分:1)

您可以将函数传递给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);

答案 1 :(得分:0)

JSON.parse reviver可用于查找键值对:

const j = '{"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': void 0, 'version': void 0, 'description': void 0, 
          'repository': void 0, 'author': void 0, 'license': void 0, 'private': void 0 }
                      
JSON.parse(j, (k, v) => k in generalInfo ? generalInfo[k] = v : v)

console.log(generalInfo)