Best Places to Keep JSON Information

As a beginner with JSON and Javascript, I am currently working on an app that requires handling multiple sets of data. With nearly 300 entries, each containing 6 properties, storing them in JSON format seems to be the most efficient approach. My goal is to retrieve this data from the app and perform various calculations.

However, I am struggling to determine the best method for storing this vast amount of data. While using global variables is convenient, it is not considered a good practice. One option I am considering is to store the data in a separate file and load it in using jQuery. Although this process may be more complex, it offers the advantage of easy updates when the entries need to be modified. I am uncertain about how this approach could impact the load times of the app.

In light of these considerations, I am seeking advice on the most preferable way or if there are alternative solutions that would work better. How would you recommend handling this situation?

Answer №1

In my opinion, storing data in a .json file and then loading it with ajax is the way to go.

This method doesn't complicate working with data at all. It's still JSON and can easily be converted into a JavaScript object.

For example:

http://jsfiddle.net/mon3419m/

$.get('https://www.reddit.com/.json').done(function(data){
    data.data.children.forEach(makeLi);
});

As for global issues..

You can simply structure your code like this:

(function(){

     //all your code.

})();

Global code can become problematic when your application is large or when you are running scripts from multiple sources. This may cause unintentional interference with your variables. But using a self-invoking function like this can help prevent that. Functions in JavaScript create new scopes. By wrapping your code in (function(){})();, you are essentially calling the function and executing it right away.

To understand this concept better, consider the following examples:

funcName(); //invokes funcName
(funcName)(); //invokes funcName
(function funcName() {})(); //invokes funcName
(function(){})(); //invokes the nameless function

Answer №2

If you want to manipulate the information in your JSON file, you'll need to come up with a JavaScript algorithm to handle serialization and deserialization of your data efficiently.

Regarding storage, you can opt for saving the details in a local file or utilize a database table with a dedicated 'JSON' column.

There are numerous options available to you; all you have to do is outline your requirements clearly.

Answer №3

Instead of relying on global objects, consider encapsulating your code within a module to improve organization and avoid conflicts. Check out AMD (Asynchronous Module Definition) for more insight.

When it comes to managing data, storing it in a JSON file and creating functions within your module to handle data loading and manipulation is highly recommended.

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 is the best way to send an array of dates to the selectedDays attribute in react-day-picker?

I am utilizing a Calendar component that employs react-day-picker to exhibit a Calendar like this: const Calendar = ({dateItems}) => { const [selectedDay, setSelectedDay] = useState(null) function handleDayClick(day) { setSelectedDay(d ...

Ways to grow your circle of friends on Facebook on a daily basis

Exploring different methods to retrieve a list of my Facebook friends on a daily basis, I embarked on a journey to find the most efficient way to achieve this goal. Initially, I opted for creating a selenium webdriver script. This involved opening a web b ...

What steps can be taken to turn off specific warning rules for CSS mode in ACE editor?

Utilizing the Ace Editor (through Brace and React-Ace) is a key aspect of my project. In our implementation, we specify the editor's mode as "css" and integrate it into our webpage. While this setup functions correctly, we have observed that some of ...

Javascript continues to conceal my web background without my permission

After loading my webpage, I click on a link and the expected javascript runs as planned. However, once it completes and presents the results, the page goes blank to a white screen displaying only the outcomes. My goal is to have these results showcased o ...

Stop auto-scrolling to the top when triggering a getJSON request

I'm feeling really puzzled at the moment. Here is the snippet of code I am struggling with: $("#combinations").on("change", "input", function (e) { e.preventDefault(); console.log(e) var $button, $row, $group, $form, $barcode ...

How can I generate 200 divs that are identical but each displaying a unique file (page0.svg, page1.svg..., page200.svg)?

<div class="mySlides"> <img src="1173/page0.svg" style="width:50%"> </div> <div class="mySlides"> <img src="1173/page1.svg" style="width:50%"> </div> My task involves generating approximately 200 similar div codes ...

Issues with retrieving information between AJAX and PHP (and vice versa)

I have a script that sends the value of a text input from an HTML form to emailform.php. This PHP file then adds the data to a .txt file. The issue I'm facing is setting the value of $email in the PHP file to match that of the HTML input field. Curren ...

Concealing a Component Until User Input is Present

My main objective is to keep one of my div elements or all my p tags hidden until there is actual user input available. The code snippet below shows my attempt at achieving this goal through a method that changes the state of the div to true or false, and ...

Fulfill the promise to retrieve the value contained within

Is there a way to use TypeScript to call the Wikipedia API for retrieving a random page title and save it in a variable for later use? I am struggling with resolving promises as I keep getting ZoneAwarePromise returned. I'm new to both promises and Ty ...

How to ensure vanilla JavaScript ES6 waits for Ajax requests to be completed

I am currently in the process of developing a website that utilizes two APIs. One issue I encountered is ensuring that my Google Maps initialization function waits for a response from one of the APIs before executing. Here is a snippet of my code: let U ...

Strange behavior exhibited by the HTML DataList component within an Angular application

I have created a simple component that displays a list of data and allows users to add new entries. <h6 *ngIf="withHeader"><label for="select"> {{title}} </label></h6> <label *ngIf="!withHeader" [ngClass]="{'required&apos ...

"Utilizing jQuery's AJAX POST method with PHP is successful, while using XMLHttpRequest is not yielding

Currently in the process of revamping my existing code to transition from jQuery's AJAX function to using XMLHttpRequest in vanilla JS. My understanding is that the following code snippets should be equivalent. However, while the jQuery version functi ...

Unlock the power of React Testing Library with the elusive waitFor function

I came across an interesting tutorial about testing React applications. The tutorial showcases a simple component designed to demonstrate testing asynchronous actions: import React from 'react' const TestAsync = () => { const [counter, setC ...

What is the best way to create a regex pattern that can effectively split two consecutive mathematical operators ("/-", "*-") from a given string equation?

Struggling to convert a string equation into an array of numbers and operators, but having trouble getting it right. Here is the regex I've tried: Input: '1+2-33/45*78'.split(/([\+\-\*\/]+)/) Output: ["1", "+", "2", "- ...

Strategies for sorting data in d3js/dimplejs visualizations

I am looking to enhance the interactivity and responsiveness of a d3js/dimplejs chart by implementing filtering based on clicks in the legends for different series. The code I tried below did not hide the series as expected, although it worked well with a ...

Encapsulate ng-style within quotation marks

I am trying to hide a span if the value of filters[*index*] is empty. var span = "<span ng-style='{ 'display': filters[" + filterIndex + "] == '' ? 'none' : 'inline-block' }'></span>" cell.html ...

Jest encountered an UnhandledPromiseRejection error because the promise was unexpectedly resolved instead of being rejected

I am facing a difficult error message from Jest that I can't seem to figure out. The error message indicates that the promise is being resolved instead of rejected, causing an unhandled promise rejection. It's confusing because Jest expects an er ...

Revise the mobile navigation opening feature to activate by clicking on a link instead of an icon

I've been struggling with a design issue for days now. I customized a template by editing its header and footer elements, incorporating Bootstrap 3 since the template was based on it. However, I recently discovered that Bootstrap 3 does not support su ...

What is the best way to interact with my component in React?

As a newcomer to Reactjs, I have a question regarding my current setup: The components in my project include navComponent.js, stackComponent.js, and nav.js I am trying to pass data from stackComponent.js to navComponent.js so that the nav.js data can be ...

Utilizing Vue's Getter and Setter to manipulate data within the frontend, rather than relying on the raw values

I am completely new to Vue and finding it difficult to understand why I am facing this issue. Whenever I make a call to my backend in order to fetch some data, the response looks like this: id: Getter & Setter name: Getter & Setter season: Getter ...