[英]Is there a way to use "js-yaml" to add one YAML file to another YAML file in nodejs unix environment
I have two YAML files.我有两个 YAML 文件。 I want to add the second YAML file inside first YAML file using "js-yaml".
我想使用“js-yaml”在第一个 YAML 文件中添加第二个 YAML 文件。 Basically my first YAML file continues to grow by adding multiple YAML files.
基本上我的第一个 YAML 文件通过添加多个 YAML 文件继续增长。
I have tried to add the new file directly to first YAML file, and it has added, but when I try it again, it is replacing the current data, not adding it:我试图将新文件直接添加到第一个 YAML 文件中,它已经添加了,但是当我再次尝试时,它正在替换当前数据,而不是添加它:
try {
var filename = path.join(__dirname, "first.yaml"),
contents = fs.readFileSync(filename, "utf8"),
first = yaml.load(contents);
var filename2 = path.join(__dirname, "second.yaml"),
contents2 = fs.readFileSync(filename2, "utf8"),
second = yaml.load(contents2);
var fruits = first.fruits ;
var newfruits = second.fruits;
first.fruits= newfruits;
console.log(util.inspect(first, false, 10, true));
fs.writeFile("first.yaml", yaml.dump(first), "utf8", err => {
if (err) console.log(err);
the above code is replacing fruits always not adding it.上面的代码总是替换水果而不是添加它。 I want to stack it when ever I want it to add.
我想在需要添加时堆叠它。
Well, I had a look on you code and there is not much to do to obtain the desired result.好吧,我查看了您的代码,无需太多操作即可获得所需的结果。
Use:采用:
first = first + second;
or:要么:
first = second;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.