Extract the value of a JSON property by passing the path as a string: property1.property2.property3

Imagine having a JSON object

let dictionary = require('../../es.json');
let obj = JSON.parse(dictionary);

Also, let's say there is a string variable

let jsonPropertyPath = 'property1.property2.property3'

Now, the goal is to retrieve the value of the property property1.property2.property3 from the obj object. Is there an efficient way to accomplish this task?

Answer №1

const Translate = (path: string): string => {
    let translationMap = require('../locale/' + context.language + '.json') as JSON;
    return eval('translationMap.' + path);
};

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

Encountering a service call error during the execution of Angular 2 unit tests using Jasmine

Struggling with testing an angular service, my TypeScript code seems correct but I keep getting an error in my test cases for "this.someFunction.show()" not being a function, even though it exists in my actual service. Here is what I'm attempting to d ...

Guide to integrating a Custom Font into live data on a PDF file with the help of jsPDF

I recently successfully converted a dynamic webpage to PDF using jsPDF and now I'm looking to customize the font family of the PDF document. Is there an option for this in jsPDF? Please advise, thank you! Here is my code snippet: <div id="#p ...

Starting the download of the canvas featuring a stylish border design

I'm facing an issue where the canvas border is not showing up when I download the canvas with a border. The border appears on the screen, but it doesn't get included in the downloaded canvas. let canvas=qrRef.current.querySelector("canvas&qu ...

Unable to determine size - collection of strings encapsulated in an interface

Having difficulty reading a string array stored within an interface. The string array is structured with multiple lines as follows: { "origem": "SERVIDOR", "codigoGrupo": "UC", "descricaoGrupo": "UC", "chave": [ "UC", "AUTOLEITURA", " ...

What is the best way to display my table?

In the index.php view, you will find my table located <table class="striped"> <thead> <tr> <th>Id</th> <th>Name</th> <th ...

Having difficulty in choosing and displaying the dropdown values

Below is the code snippet: WebElement pooldropdown=driver.findElement(By.xpath("/html/body/section/section[2]/div[1]/select")); Select sel = new Select(pooldropdown); List<WebElement> list = sel.getOptions(); System. ...

"Troubleshooting a jittery React sticky header during scrolling: Dealing with IntersectionObserver and CSS

My current project involves implementing a sticky header in a React application using the Carbon framework. The goal is to have a sticky header with a bottom border when users scroll up, providing context for their viewing experience. However, when they sc ...

Struggling to remove files in Alfresco as Lucene search continues to display 1000 results, even after deleting nodes

Dealing with Alfresco Version 3.3 - In my JavaScript program within the Alfresco Repository, I am attempting to delete all archived files similar to Windows' Recycle Bin. However, I have encountered an issue where the Lucene search only retrieves 100 ...

Tips for creating different CSS print layouts within a single-page application

I am working on a single page application with multiple sections that need to display a list of images on separate pages. I was considering using iframes for this task, but I'm looking for advice from someone who has experience with similar situations ...

Having issues with Bootstrap 5 modal not functioning correctly?

I'm facing an unusual issue that I'm struggling to resolve. I'm currently utilizing Bootstrap 5 and encountered the following problem: const reportBtns = Array.from(document.querySelectorAll('.report')) reportBtns.forEach((btn) ...

Dealing with errors in external URLs with AngularJS: A guide to intercepting errors in $resource or $http services

I have set up a separate server for my angularjs app, which is different from my grunt server. This external server runs on Apache and uses PHP to serve JSON data. I want to be able to catch any potential server errors, ranging from "server down" to "404". ...

I am unable to utilize useContext in my React app with TypeScript

I am a beginner in TypeScript and I'm currently working on developing a Todo application using the 'useContext' hook in TypeScript-React. I'm facing an issue while trying to pass the TodoContextProviderProps as a prop below to my Provi ...

What causes an error when attempting to add a new user to the database?

Currently, I am delving into the world of Mongodb. To kick things off, I initiated by executing npm install --save mongoose uuid within the confines of Terminal. The primary objective of my project revolves around storing user information in the database. ...

Issue: The login.component.html file failed to load

I attempted to utilize a custom-made HTML file with the templateUrl attribute in Angular2. Below is the content of my login.component.ts file: import {Component} from '@angular/core'; @Component({ selector: 'login' , template ...

Using a regular function as a jQuery value in jQuery

I am attempting to retrieve the value 'col' that is specified in the HTML code, and then use it as the background color for an element. Here is the JavaScript function: function setBackgroundColor(col) { $("#BG").animate({ backgroundCol ...

The function that iterates through the 'categoria' state and returns a new array is not functioning properly

Having difficulty with the object of a function using .map(). It works when the code is used directly, but not when put inside a function. For example: if(this.state.cat){ return _.map(this.state.cat, categoria => { if(this.state.search_ ...

Unable to Add Dependency to Subclassed Object

In my setup, there are three classes that interact with each other. I am utilizing inversify for handling dependency injection. However, I encountered an issue when I injected the class MessageBroker into the derived class Repository and found that the Mes ...

Adding pointlights to the camera in three.js is a great way to

I have written some code to add two lights on the camera that move along with the orbitcontrols. However, the lights and spheres are not visible for some reason. Can someone help me identify the issue in my code? var sphere1 = new THREE.Mesh( new THREE.Sp ...

What is the best way to determine the number of objects in a list based on

When I deserialize a JSON like this: files = JsonConvert.DeserializeObject<Files>(json); I am trying to count the instances of natives-windows and artifact, so I have used lambda expressions, but I keep getting a NullReferenceException. files.libra ...

IndexedDB is not supported for storing pages on IE10

This is an example of a demo application using IndexedDB. It runs smoothly on my IE10 when accessed through the web. However, if I save the file (named index.html) and try to open it, a JavaScript dialog box appears due to the following code: window.inde ...