[英]Import all files from a folder in VSCode Studio Code Webview API
我正在導入 json 文件如下:
import input1 = require("../test/test1.json");
import input2 = require("../test/test2.json");
import input3 = require("../test/test3.json");
import input4 = require("../test/test4.json");
import input5 = require("../test/test5.json");
我的 tsconfig 設置是:
"module": "commonjs",
"target": "es6",
但是我需要導入包含大量 json 文件的整個“test”文件夾。 如何導入所有文件並將每個文件分配給“輸入”變量?
更新:
我已經嘗試了@Michael 建議的以下代碼。 但是給出了以下錯誤。
const fs = require('fs');
let testDataPath = "../test"
let filenames = fs.readdirSync(testDataPath)
filenames = filenames.filter(it => it.endsWith(".json"))
let runvalue = [];
for(let filename of filenames) {
let file = JSON.parse(fs.readFileSync(testDataPath + "/" + filename, "utf-8"))
let json = Object.values(file["covered-points"]);
runvalue = [...runvalue, new Set(json)]
}
但它給出錯誤: "Uncaught TypeError: fs.readdirSync is not a function"
錯誤: "Uncaught TypeError: fs.readdirSync is not a function"
我無法弄清楚 Visual Studio 代碼中的“fs”有什么問題。 有人請幫助我。 感謝您的時間。
假設您使用的是 NodeJS 而不是 Web 瀏覽器,最好的方法是使用fs
核心模塊讀取目錄內容,然后讀取文件內容。 例如:
let testDataPath = "./test"
let filenames = fs.readdirSync(testDataPath)
filenames = filenames.filter(it => it.endsWith(".json"))
for(let filename of filenames) {
let json = JSON.parse(fs.readFileSync(testDataPath + "/" + filename, "utf-8"))
//do something with the json
}
筆記:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.