Javascript 使用节点js动态构建JSON
我在特定文件夹中有(n)个JSON文件: 为了简单起见,让我们假设3个JSON文件Javascript 使用节点js动态构建JSON,javascript,node.js,json,Javascript,Node.js,Json,我在特定文件夹中有(n)个JSON文件: 为了简单起见,让我们假设3个JSON文件Animal.JSON、Noun.JSON、POS.JSON 其内容分别为 Animal.json Noun.json POS.json 我希望能够循环遍历特定文件夹中的所有JSON文件,并以以下格式动态构建JSON label: { Dinosaur: { isA: 'Animal' }, Lion: { isA: 'Animal'
Animal.JSON、Noun.JSON、POS.JSON
其内容分别为
Animal.json
Noun.json
POS.json
我希望能够循环遍历特定文件夹中的所有JSON文件,并以以下格式动态构建JSON
label: {
Dinosaur: {
isA: 'Animal'
},
Lion: {
isA: 'Animal'
},
Tiger: {
isA: 'Animal'
},
Animal: {
isA: 'Noun'
},
Noun: {
isA: 'POS'
}
},
sample: {
'#Noun rex|saurus': 'Dinosaur',
'simba': 'Lion'
'big cat': 'Tiger',
'Herbivore|Carnivore' : 'Animal',
'Proper|Common' : 'Noun'
}
到目前为止,我的逻辑是:
function buildJSON() {
fs.readdirSync('/path/to/file').forEach(file => {
const path = '/path/to/file' + file;
const data = fs.readFileSync(path);
const txt = JSON.parse(data);
console.log(JSON.stringify(txt)); //Displays content of each file
/* I need the logic to build the lable and sample for the output json */
});
}
非常感谢您的帮助/指导。您只需使用
Array.reduce()
创建地图:
let animal=[{“标签”:“恐龙”,“样本”:[“#noon Rex”,“saurus”]},{“标签”:“狮子”,“样本”:[“辛巴”]},{“标签”:“老虎”,“样本”:[“大猫”]};
让名词=[{“标签”:“动物”,“样本”:[“食草动物”,“食肉动物”]};
让pos=[{“label”:“Noun”,“sample”:[“property”,“Common”]}];
函数getResult(arr、isA、result){
结果=arr.reduce((a,curr)=>{
a、 label=a.label | |{};
a、 标签[当前标签]={
“isA”:isA
};
a、 sample=a.sample |{};
a、 sample[当前sample.join(“|”)=当前标签;
返回a;
},结果);
返回结果;
}
让结果={};
getResult(动物,“动物”,结果);
getResult(名词,“名词”,结果);
getResult(pos,“pos”,result);
控制台日志(结果)代码>除了现有代码之外,我还添加了构建所需输出的逻辑
function buildJSON() {
// empty result
let result = { label: {}, sample: {}};
fs.readdirSync('/path/to/file/').forEach(file => {
const file_path = '/path/to/file/' + file;
const data = fs.readFileSync(file_path);
const items = JSON.parse(data);
// remove .json extension, this will be used to construct labels
const file_name = file.replace(/\..+$/, "");
// loop through each item in the json file
for(let item of items) {
// construct labels
result.label[item.label] = { isA : file_name }
// construct samples
result.sample[item.sample.join("|")] = item.label;
}
});
return result;
}
非常感谢。这很有效。是否可以将它们作为单独的行项目列出,而不是(|)
管道<代码>示例:{'noon rex | saurus':'恐龙','辛巴':'狮子','大猫':'老虎','食草动物':'动物','食肉动物':'动物','特有':'名词','普通':'名词'}
这可行吗?你有机会看一下吗?代码已经接近你所需的80%,这将是一个很好的练习,你可以做进一步的修改,使其达到100%
label: {
Dinosaur: {
isA: 'Animal'
},
Lion: {
isA: 'Animal'
},
Tiger: {
isA: 'Animal'
},
Animal: {
isA: 'Noun'
},
Noun: {
isA: 'POS'
}
},
sample: {
'#Noun rex|saurus': 'Dinosaur',
'simba': 'Lion'
'big cat': 'Tiger',
'Herbivore|Carnivore' : 'Animal',
'Proper|Common' : 'Noun'
}
function buildJSON() {
fs.readdirSync('/path/to/file').forEach(file => {
const path = '/path/to/file' + file;
const data = fs.readFileSync(path);
const txt = JSON.parse(data);
console.log(JSON.stringify(txt)); //Displays content of each file
/* I need the logic to build the lable and sample for the output json */
});
}
function buildJSON() {
// empty result
let result = { label: {}, sample: {}};
fs.readdirSync('/path/to/file/').forEach(file => {
const file_path = '/path/to/file/' + file;
const data = fs.readFileSync(file_path);
const items = JSON.parse(data);
// remove .json extension, this will be used to construct labels
const file_name = file.replace(/\..+$/, "");
// loop through each item in the json file
for(let item of items) {
// construct labels
result.label[item.label] = { isA : file_name }
// construct samples
result.sample[item.sample.join("|")] = item.label;
}
});
return result;
}