程式語言 - Node.js - Read YAML



參考資訊:
https://www.npmjs.com/package/js-yaml

main.js

var fs = require('fs');
var yaml = require('js-yaml');

const yfile = yaml.load(fs.readFileSync('test.yaml', 'utf8'));

Object.keys(yfile).forEach(k0 => {
    var v = yfile[k0];
    const len = v.length;

    console.log(k0);
    for (var i=0; i<len; i++) {
        Object.keys(v[i]).forEach(k1 => {
            console.log('desc:', v[i][k1]['desc']);
        });
    }
});

test.yaml

this is title:
    - test 1:
        desc: "description for test 1"
    - test 2:
        desc: "description for test 2"

執行

$ node main.js
    this is title
    desc: description for test 1
    desc: description for test 2