Is there a way to retrieve a secondary JSON data set using a variable within the primary JSON object when working with Handlebars?

My project involves working with two datasets - one defines the included items for a page, while the other contains deeper data. To simplify, let's refer to the first dataset as page.json:

{
 "title": "pageTitle here",
 "description": "Content here",
 "items": [
  {
   "id": 14,
   "name": "Bob"
  },
  {
   "id": 11,
   "name": "Dave"
  },
  {
   "id": 12,
   "name": "Fred"
  }
 ]
}

The second dataset, known as data.json, includes:

[
 {
  "id": 14
  "name": "Bob Matthews",
  "description": "Some description here about Bob",
  "age": 24,
  "location": "Sydney"
 },
 {
  "id": 11
  "name": "Dave Smith",
  "description": "Some description here about Dave",
  "age": 23,
  "location": "Sydney"
 },
 {
  "id": 12
  "name": "Fred Williams",
  "description": "Some description here about Fred",
  "age": 42,
  "location": "Sydney"
 }
]

In the templates, I attempt to display the data like so:

<!-- Loop through each person for this page from page.json -->
{{#each page}}
  <!-- Find and display data for this person from data.json -->
  {{#compare page.id data.id}}
   <h2>{{data.name}}</h2>
   <p>Description {{data.description}}</p>
   <p>Age {{data.age}}</p>
   <p>Location {{data.location}}
  {{/compare}}
{{/each}}

However, this method does not work. Is there a way without creating a custom handlebar helper to access and display data from an item in a separate JSON data source array?

Note: The project uses Assemble.io & Grunt to build static pages from JSON data sets.

Thank you!

Answer №1

I haven't personally tested this method, but have you considered iterating through the contents of the list in data.json to locate the element that corresponds to the current element in page.json? It's possible that some adjustments might be needed as my expertise lies more with YAML Front Matter rather than working with *.json files.

{{#each page.items}} <!-- looping through each individual for this specific page from page.json -->
    {{#each data}}   <!-- iterating over each person in the list from data.json -->
        <!-- In this section '../page' references the previous scope's page -->
        <!-- while 'this' points to the current element in data.      -->

        <!-- let's retrieve and display the information for this person from data.json -->
        {{#compare ../page.id this.id}}
            <h2>{{this.name}}</h2>
            <p>Description {{this.description}}</p>
            <p>Age {{this.age}}</p>
            <p>Location {{this.location}}    
       {{/compare}}

    {{/each}} <!-- ending data iteration -->
{{/each}}     <!-- finishing page.items loop -->

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be the reason why my AJAX error is not displaying the exception from my Webmethod?

I am currently utilizing Google Chrome for my work tasks. After referring to , I attempted to throw an exception from a webmethod. However, no output is shown in the console.log. The only information I received is a generic error message from the network ...

What is the best way to design a circular icon using OpenLayers?

I am currently incorporating openlayers into my ionic app and working on placing users on the map. I have encountered a problem as I am unsure how to apply custom CSS styling to the user element, which is not directly present in the HTML. In the screenshot ...

Is there a way to extract the value of a child object from an object within the MUI Datagrid

Using the following method: useEffect(() => { PetService.getAll("/pets") .then(response => { setData(response.content); }) }, [setData]); The response obtained is as follows: { "timestamp": 167818109 ...

What is the best way to calculate the variance in data added to a Python dictionary?

Given the dictionaries: dict1 = { "A" : ["a","b","c"]} # old one dict2 = { "A" : ["a","b","c","d"]} # new one #I want to achieve: dict3 = {"A":["d"]} Traditionally, using 'deepdiff' is not suitable as the dictionary architecture needs to remain ...

New to Python? Learn how to easily convert complex JSON data into a CSV file with this beginner

My understanding of Python programming is still at a basic level, and I'm facing an issue with handling a JSON file obtained from the developer API for League of Legends. The JSON file contains information about various game characters and their stats ...

Concealing and revealing an element using the CSS property visibility:hidden/visible

There are two div-boxes that should show/hide when clicking on another two div-boxes. I want the divs to maintain their space so as not to disrupt the DOM, ruling out the use of .toggle(). I attempted this approach without success: $('#red, #pink&ap ...

Setting default zoom level for Google Map in HTML

Initially, the map for the second city is hidden. When the second button is clicked, the map for the first city will be hidden and the map for the second city will be displayed. However, in this case, the map for the second city appears zoomed out. How can ...

What steps can I take to ensure my text displays in a single line?

To enter the prize draw, simply click this link and like us on our Facebook page. Your chance to win awaits! Below is the code snippet: function popupWin() { text = "<html>\n<head>\n<title>Pop Window</title>\n<b ...

The designated origin in JavaScript is not displaying in the Django template

Sorry for the possibly silly question, but as I delve into Javascript and Django, I'm struggling with a specific issue. Despite spending hours on it, I can't seem to figure out why my image isn't displaying in my Django HTML template. Here ...

jQuery's event delegation feature fails to work in conjunction with AJAX requests

I'm currently facing issues with jQuery's on event delegation: Below is the code snippet I am working with: HTML portion: <body> <!-- Page Content Wrapper--> <div id="sb-site"> <div id="overlay">& ...

Tap on a Gridview item to display the corresponding image in an ImageView

After following a tutorial on Android Lazy Loading images and text in a listview from http json data, I have now converted the listview to a gridview. My question is: How can I click on a gridview item and display the image in an imageview? MainActivity ...

When the function $(document).width() is called, it may yield varying results depending on the timing of the call

Every time I use $(document).width(); within $(window).load(function(){}) or $(document).ready(function(){}), the result differs from when it is called inside $(window).resize(function(){}). The discrepancy is approximately 100px, and it's not due to ...

How can you style a two-item list in Material-UI React to display the items side by side?

I have a list that contains two items: 'label' and 'value'. This is the current layout: https://i.stack.imgur.com/HufX7.png How can I rearrange the items on the right to be positioned next to the label on the left? https://i.stack.im ...

TestingCompilerFactory is not available as a provider

Currently troubleshooting my test file to identify the issue that is hindering a successful test run: import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { Component, Directive, Input, OnInit } from '@angula ...

Arranging by and loading progressively in AngularJS

Encountering an issue with the orderBy function in an ng-repeat paired with an auto-incrementing limitTo. After loading a few elements on the page, the directive ceases to function properly and stops increasing the element limit. Here is the HTML code sni ...

Protractor map function fails to provide a defined output

When working with an application containing multiple widgets, each with its own title and content, I want to effectively map the elements of each widget for easier testing purposes. For instance, let's consider a page: this.widgets = element.all(by. ...

"Oops, we hit a snag" notification appears when attempting to share on Facebook using the share dialog

I am currently integrating Django with Facebook for page sharing. Below is the code snippet I am using to form the URL: var link = 'https://www.facebook.com/dialog/feed?app_id=1234567890&display=popup&name=' + name + '&descripti ...

What is the process of connecting marionette rendered views to the DOM?

I am encountering a challenge with my simple setup using Marionette that I am trying to resolve. My issue lies within a view containing a collection: var MyItemsView = Marionette.View.extend({ template: "#some-template" }); var view = new MyItemsVie ...

Mesh does not display texture in three.js

I've been attempting to add a texture to my sphere mesh using the code snippet below: var loader = new THREE.TextureLoader(); var balltex = loader.load("pic1.jpg"); var ballmat = new THREE.MeshBasicMaterial({ map: balltex }); var ballgeo = new THREE ...

Tips for placing multiple images onto one canvas

I am currently working on a web application that allows users to create sketches, define their positions and sizes, and then save them to a database. In another section of the website, I aim to retrieve these images and display them on a single canvas. To ...