Exploring the versatility of JSON for data manipulation in JavaScript as akin to working with a relational SQL

Looking at this JSON variable:

var peopleList = {
    "1": {"Name": "Lisa", "item1": "Name of Item 1"}   ,
    "2": {"Name": "Marty"}   ,
    "3": {"Name": "Jordan",  "item1":"Name of Item 1",  "item2":"Name of Item 2"}
}

This structure seems similar to a relational database.

Currently, I am working on a program where I need to add multiple people (1-3) and assign multiple items to each person in this variable.

One challenge I am facing is determining the getters and setters in JavaScript for this variable.

For instance, how can I add an item to "Marty"?

Also, how do I retrieve the Name for item2 belonging to "Jordan"?

I appreciate any assistance as I am still learning about JSON.

If there is a simpler way to achieve this goal that is easier to work with, I am open to suggestions.

Answer №1

To retrieve the value of any object using dot notation:

 peopleList[3].Name // this will give you "Jordan"

If you want to add a key-value pair to an object, you can follow this example:

var MartyData = peopleList[2];
MartyData['item1'] = "Name of item 1 of Marty";
// you can then check if the data has been added
alert(peopleList[2].item1); // this will return "Name of item 1 of Marty"

Check out the DEMO

Answer №2

JSON Object can be compared to a Java Bean or Map structure.

var peopleList = {
'one':{'name':'Marty','item1':'Name of item1'},
'two':{'name':'Jack','item1':'Name of item1'},
'three':{'name':'Jordan','item1':'Name of item1','item2':'Name of item2'}
}

For instance, how do I insert an item for "Marty"

Also, how do I retrieve the Name for item2 of the individual "Jordan"

Insert an item for "Marty"

peopleList.two.item1 = "Name of item1"

Retrieve the Name for item2 of the individual "Jordan"

alert(peopleList.three.item2);

===================================================
Apologies for the mistake. Corrected.

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

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 ...

Vanish Dropdown upon clicking Using JavaScript

Hey, I'm new to web development and I'm currently working on creating a web app for iPhone. Everything is going smoothly except for one issue - my dropdown menu works perfectly on desktop Chrome, but on the iPhone's Safari browser, I can&ap ...

Display a component just once in React or React Native by utilizing local storage

I am struggling with a situation where I need to display a screen component only once using local storage. It's really frustrating. App.js ... constructor(props) { super(props); this.state = { isLoading: false, }; } component ...

Manage a frame directly from the parent page

I am faced with a situation where I have a web page with two textboxes and a button. Unfortunately, I am unable to alter the contents of this page in any way. My goal is to find a way to automatically populate the first textbox with some text when the pag ...

How come submitting a form without refreshing does not update the database with new values?

I'm encountering an issue with my form and script that is supposed to connect to the operation.php class. Despite having everything set up correctly, the data is not being added to the database and the page does not refresh. I'm perplexed as to ...

Tips for accessing Ajax data within Ember computed property

I'm facing a challenge with returning data from an Ajax call in a computed property. Despite being aware of the asynchronous nature, I am unable to figure out how to do it due to the specific requirement of returning the data in an array format with o ...

javascript implementing number formatting during keyup event

When I try to format a number in an input field on the keyup event, I receive a warning in my browser console that says "The specified value "5,545" cannot be parsed, or is out of range." The value in the input field also gets cleared. How can I solve this ...

Ensuring validity using dynamic context objects within Joi

I need to implement a dynamic validation system that involves downloading an object at runtime and saving it as a well-formed .json file. The objective is to use the values from this downloaded object as part of a validation process using Joi.validate wi ...

Leveraging the power of Notepad++

When I attempt to use Notepad++ for Javascript, it isn't functioning as expected. Instead of displaying the proper outcome on the web page, all I see is a jumbled mess. Currently, I am using version 6.6.7 of Notepad++. Here's my process: Typ ...

Detecting the specific button that was selected with PHP

I am in the process of developing a website for a production company where, upon clicking on a director's name from the menu, all other menu items disappear and only the selected director's biography and work are displayed. My current challenge ...

Retrieve images of the webpage discreetly and with seamless execution using JSON AsyncTask

I am a beginner in the world of Android development. Currently, I am working on a project where I need to retrieve data from a website and display it in a listview. My approach involves getting String data using httpGet, parsing it with json, and then pop ...

Configuration for secondary dependencies in Tailwind

As per the guidelines outlined in the official documentation, it is recommended to configure Tailwind to scan reusable components for class names when using them across multiple projects: If you’ve created your own set of components styled with Tailwin ...

"Creating visual art with processing in 2D using P5

Recently, I came across a webpage about rendering 2D objects in processing using JavaScript. Here is the link to the page: Upon trying out the example code provided on the page in a new P5 project, I noticed that the code structure looked like this: HTML ...

Implement various actions when the window is resized

The carousel code I have found returns a different number of items to display based on screen size. You can check it out here. $(function() { var mini = 1000; var big = 1280; if (window.screen.availWidth < mini) { $('#carousel1').jsCar ...

Improving the innerHTML syntax

I am looking for the correct syntax to add content to an element using innerHTML. Here is an example that isn't working as expected: openNewWindow: function(content) { popupWin = window.open(content, 'open_window', 'menubar, ...

jQuery MaskMoney - position input at the start of the field

Check out this jsfiddle I created to showcase the issue I'm facing. Whenever I click on the input field, the cursor automatically goes to the end. This means that I have to start typing cents before dollars, which is not the behavior I want. My desire ...

Converting JSON data into a jQuery-powered spreadsheet

Recently, I completed a module on data visualization, where I learned how to transform Google Spreadsheets into JSON using jQuery. In my spreadsheet, there are two simple columns: date and status (collected data about myself for practice in visualizing it ...

The regular expression for validating credit card numbers is invalid due to a repetition error

Here is the regular expression I've been using to validate credit card numbers in JavaScript: var match = /^(?:(4[0-9]{12}(?:[0-9]{3})?)|(5[1-5][0-9]{14})|?(6(?:011|5[0-9]{2})[0-9]{12})|(3[47][0-9]{13})|(3(?:0[0-5]|[68][0-9])?[0-9]{11})|((?:2131|1800 ...

Creating a personalized and versatile table component with unique functionality: where to begin?

Looking to create a versatile table component that can adapt for both desktop and mobile versions. If the screen width is below 720px, it should display a table using div, ul, li elements with a "load more" button at the bottom. If the screen width i ...

Mask the "null" data in JSON

I'm currently working with a JSON file and trying to display it in a bootstrap table. Check out the code snippet I've been using: $(document).ready(function(){ $.getJSON(url, function(data){ content = '<h1><p class="p1 ...