Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

107
Views
Mustache accessing parent object info from child object

Mustache: Is there any way to get parent info from child? My data structure is like:

data = {
    peerOrgs: [
         {
             org: "Org1",
             peers: [{name:"peer0"}, {name:"peer1"}]
         },
         {
             org: "Org2",
             peers: [{name:"peer0"}, {name:"peer1"}]
         }
    ]
}

Now I want to write ${name}.${org} over peers of each peerOrgs. i.e.

peer0.Org1
peer1.Org1
peer0.Org2
peer1.Org2

.js file:

var Mustache = require("mustache")
var fs = require("fs")

var data = {
    peerOrgs: [
         {
             org: "Org1",
             peers: [{name:"peer0"}, {name:"peer1"}]
         },
         {
             org: "Org2",
             peers: [{name:"peer0"}, {name:"peer1"}]
         }
    ],
    getOrgFromPeer: function() { return `${super.org}` }
}
var template = fs.readFileSync(mustacheFile).toString()
var output = Mustache.render(template, data)

.mustache file:

{{#peerOrgs}}
  {{#peers}}
     {{name}}.{{getOrgFromPeer}}
  {{/peers}}
{{/peerOrgs}}

But I got

enter image description here

Is there any way to get parent info from child?

----------------------edit------------------------- A workaround:

var data = {
    peerOrgs: [
         {
             org: "Org1",
             peers: [{name:"peer0"}, {name:"peer1"}]
         },
         {
             org: "Org2",
             peers: [{name:"peer0"}, {name:"peer1"}]
         }
    ],
    getOrgFromPeer: function() {
        for (const peerOrg of config.peerOrgs) {
            for (const peer of peerOrg.peers) {
                if (peer==this) {
                    return `${peerOrg.org}`
                }
            }
        }
    },
}

Feasible anyway.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!