Merging text and a JSON object to retrieve the information

Having some trouble with a JSON object and retrieving values.

This is the syntax that works for getting the data I need.

dataJSON.companies[0].fields.Internet.length

I want to dynamically evaluate the object using a string variable, like this...

var mediaType = 'Internet'; 
dataJSON.companies[0].fields+mediaType+.length;

Is there any way to achieve this? I'm stuck.

Answer №1

When you are unsure of the property name until runtime, utilize square brackets access notation.

For example:

var mediaType = 'Internet'; 
dataJSON.companies[0].fields[mediaType].length;

You have the option to access object members using dot notation or square brackets notation.

For instance:

var result = myobject.propertname;

or

var result = myobject["propertyname"];

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

How can one effectively manage the disparities between JSON and Python while deserializing data?

Currently, my task involves working on a messaging service that utilizes a python based API. The API handles most of the deserialization work by transforming messages into python dictionaries. However, there are instances where the resulting dictionary con ...

Having trouble with importing a variable in an Express application? You may encounter this error message: "Route.get() must

When trying to import requireSignin from the controllers/auth.js file into the routes/user.js file and adding it to the router.get('/user/:id', requireSignin, read); route, an error occurs: Error: Route.get() requires a callback function but r ...

How can I access the value of a textbox within a dynamically generated div?

In my project, I am dynamically creating a div with HTML elements and now I need to retrieve the value from a textbox. Below is the structure of the dynamic content that I have created: <div id="TextBoxContainer"> <div id="newtextbox1"> // t ...

VueDraggable communicates with the database by sending a request during drag and drop interactions

Help needed with the vuedraggable component. I have three columns (photo attached) and I would like to be able to drag BoardUserCard between the columns. Upon dropping the card, I want to send a PUT request to the database to change the "lead_status_id" as ...

Error arising from attempting to compile React animations for a specific component, rc

As someone new to React, I decided to try running a sample example from the following link: To do this, I created a file named MonApp.js with the code snippet below: import React, { Component } from 'react'; import { render } from "react-dom"; ...

On the server side, the received Req.body appears as an empty object: { }

import { Injectable } from '@angular/core'; import { Http, XHRBackend, RequestOptions, Request, RequestOptionsArgs, Response, Headers } from '@angular/http'; import { Observable } from 'rxjs/Observable'; impo ...

Refreshing of JSON data does not occur

I'm currently working on developing an Android app that involves JSON Parsing. I have a php script hosted on my server that fetches data from a MySQL database and encodes it to JSON. The app communicates with the server to retrieve this data and popul ...

When attempting to send data from Ajax to PHP as JSON, an error occurs and the data transmission fails

When attempting to send data as JSON, an error response is received stating: SyntaxError: Unexpected token < in JSON at position 0. If the data is sent as text, it successfully reaches PHP, but PHP does not respond accordingly. AJAX/JavaScript using J ...

Examples for Foursquare using asp.net

As someone who is new to JSON, I find myself a bit puzzled. My goal is to utilize userless access for the foursquare api in order to retrieve venue information for a specific location. Can you provide me with a comparable example using .net? ...

Guide on converting JSON to CSV in React by utilizing the map function

When I convert JSON data to CSV by clicking a button, it currently stores the data in the CSV file separated by commas. However, I want each piece of data to be on its own line. How can I achieve this? For example: Minor,Minor What I Want: Each item on a ...

Double the Power of jQuery Live Search with Two Implementations on a Single Website

I recently implemented a JQuery Live Search feature that is working perfectly. Here is the JQuery script: <script type="text/javascript"> $(document).ready(function(){ $('.search-box input[type="text"]').on("keyup input", function(){ ...

What is the process for obtaining a compilation of JavaScript files that are run when an event is triggered?

How can I generate a list of all the JavaScript files that are triggered when a button is clicked or an input field is selected in a complex system? Is it possible to achieve this with Chrome DevTools, or are there alternative solutions available? If Chro ...

JSON Schema: Mandate a property if any nested item contains a different property

Within my dataset, I have the following structure: { "ELEMENTS": { "element_1": { "requiredPropIfAtLeastOneFlag": "", "ITEMS": { "item_1": { "flag": & ...

Is it possible to include indices in a list of hashes when sending an Ajax request to the controller in JSON format?

When sending data to a Rails controller: $.ajax({ url: '/haha', type: 'put', dataType: 'json', data: { multiplication: [ { "a": 5, "b": 5 }, { "a": 5, "b": 5 ...

What is the process for exporting a plugin from dayjs() in JavaScript?

Currently, I have incorporated the plugin isToday() to enhance the capabilities of dayjs(). Nevertheless, I am uncertain about how to export isToday() in order to utilize it across other files. import isToday from "dayjs/plugin/isToday"; expor ...

Ways to send data to a popup in svelte

Hey there, I could really use some assistance with my Svelte app. I'm trying to implement a modal and pass a parameter to the modal component in order to customize its content. However, when using the open() function of Modal, we only need to provide ...

Exploring how to use React with a select component featuring objects

Hello, I am new to working with React and I have a question regarding the select component of Material UI. Here's my situation: I am creating functionality for creating and editing a User object. This User object has a primary key and some data, incl ...

Fixing the slide bar in React using styled components

In the early stages of creating a slider where cards (divs) can be moved left and right with a click, I encountered an issue. The onClick handler is functioning properly. However, upon running the project, I noticed that the cards start 230px away from the ...

Utilizing the Google Translate API within an ASP MVC framework to translate a div's content from English to Arabic

Currently, I am working on a small project that involves two divs: one for English and another for Arabic. Despite creating the project, I am encountering an issue with getting the translation from English to Arabic. Below is the code I have attempted, but ...

The values of React children props will always remain consistent

While attempting to incorporate an ErrorBoundary HoC component for error handling following the guidelines from React16 documentation, I designed the ErrorBoundary component as a PureComponent. It became apparent that the children props remained consistent ...