Locate data objects using JavaScript keys

My object looks like this:

{
  sunday: {status: "open", opening_time: "11:30 am", closing_time: "12:00 pm"},
  monday: {status: "close", opening_time: "", closing_time: ""},
  tuesday: {status: "close", opening_time: "", closing_time: ""},
  wednesday: {status: "close", opening_time: "", closing_time: ""},
  thursday: {status: "close", opening_time: "", closing_time: ""},
  friday: {status: "close", opening_time: "", closing_time: ""},
  saturday: {status: "close", opening_time: "", closing_time: ""}
}

The current data is stored in a variable called var curr = 'friday'. I want to retrieve only the friday object from the collection, which is

friday: {status: "close", opening_time: "", closing_time: ""}
.

What would be the best approach to achieve this?

Answer №1

Here is an example of how to use it:

let schedule = {
  sunday: {status: "open", opening_time: "11:30 am", closing_time: "12:00 pm"},
  monday: {status: "close", opening_time: "", closing_time: ""},
  tuesday: {status: "close", opening_time: "", closing_time: ""},
  wednesday: {status: "close", opening_time: "", closing_time: ""},
  thursday: {status: "close", opening_time: "", closing_time: ""},
  friday: {status: "close", opening_time: "", closing_time: ""},
  saturday: {status: "close", opening_time: "", closing_time: ""}
};
let currentDay = 'friday';
console.log(schedule[currentDay]);

Answer №2

To determine if a key exists within an object, perform a check and then access the corresponding value using either dot notation or square brackets.

let schedule =  {
           sunday: {status: "open", opening_time: "11:30 am", closing_time: "12:00 pm"},
           monday: {status: "close", opening_time: "", closing_time: ""},
           tuesday: {status: "close", opening_time: "", closing_time: ""},
           wednesday: {status: "close", opening_time: "", closing_time: ""},
           thursday: {status: "close", opening_time: "", closing_time: ""},
           friday: {status: "close", opening_time: "", closing_time: ""},
           saturday: {status: "close", opening_time: "", closing_time: ""}
       }
var currentDay = 'friday';
if (currentDay in schedule)
{
   console.log(schedule[currentDay])
}

Answer №3

When working with JavaScript Objects, you can access values using a key by using the normal index notation like myobj['key']

var myobj = {
  sunday: {status: "open", opening_time: "11:30 am", closing_time: "12:00 pm"},
  monday: {status: "close", opening_time: "", closing_time: ""},
  tuesday: {status: "close", opening_time: "", closing_time: ""},
  wednesday: {status: "close", opening_time: "", closing_time: ""},
  thursday: {status: "close", opening_time: "", closing_time: ""},
  friday: {status: "close", opening_time: "", closing_time: ""},
  saturday: {status: "close", opening_time: "", closing_time: ""}
}

console.log(myobj['friday']);

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 Occurs to Processed Messages in Azure Functions if Result is Not Output?

I have created an Azure function that is connected to the messaging endpoint of an IoT Hub in order to trigger the function for all incoming messages. The main purpose of this function is to decompress previously compressed messages using GZIP before they ...

Monitoring changes in the size of the parent element with an AngularJS directive

Issue I am facing a challenge with a directive that updates the size of an element based on the window size. The directive monitors changes in window dimensions and adjusts the element accordingly. MyApp.directive('resizeTest', ['$window&a ...

Is it possible to render an SVG using PDFTron?

Before, I attempted to utilize an Annotation.StampAnnotation to make a personalized annotation while using an SVG as the foundation image. Unfortunately, I discovered that the StampAnnotation does not allow the user to alter or set the color. Thus, I have ...

Is there a way to align my two tables next to each other using CSS?

.page { display: grid; grid-template-columns: 1fr 1fr; grid-gap: 20px; } .items { float: left; } .secondItem { vertical-align: text-top; float: right; } .page-header table, th, td { border: 1px solid; display: block; background-color: ...

Transmitting multiple file attachments to a PHP file through AJAX

In my form, I have the following HTML code: <input type="file" id="attachments" name="attachments" multiple> I've already implemented a JavaScript function that handles form submission using AJAX, but it doesn't handle uploaded files. Wi ...

hide bootstrap confirmation when clicking off of it

I'm facing an issue with the bootstrap confirmation plugin. I want to close all confirmations when the user clicks outside of the confirmation pop-up. However, the current implementation is causing the confirmation to close everytime there is a click ...

Unusual glitch found in Angular application's JavaScript code

I am completely puzzled by this issue: I have a Angular app and I'm trying to update the class using a simple JavaScript string update where the class name is bound to the DOM. The strange thing is that in my application, the most basic statement for ...

Clicking our way back through the loop

Looking to display a name when each item is clicked. I've assigned an ID to each element, but it seems like I'm overlooking something. In the current code, I'm thinking there should be a variable involved, but I'm uncertain: $(document ...

When transmitting data from the parent component to the child component, the data successfully passes through, yet the view fails to reflect the

I'm facing an issue with passing data from a parent component to a child component using props. Here is the code snippet I'm working with: <div id="root"> <my-component v-bind:account-types='accountTypes'> </my-c ...

Analyze the individuals listed in one column of the table and calculate the total from the adjacent column using JavaScript

I have a table with participant data that I would like to compare. If a participant has multiple result points in the table, I want a script to calculate the sum of all their results. This process should be repeated for each participant listed in the table ...

What's the best way to loop through each touch event property within the TouchList of a touch event using JavaScript?

I'm struggling to grasp touch events, as nothing seems to be working for me. For testing purposes, I've created a very basic page: HTML: <div id="TOUCHME"> TOUCH ME </div> <div id="OUTPUT"></div> Jav ...

Break down one object into an array consisting of multiple objects

I have a single object in the following array: [{"IP_Address":"33.4.160.5","originEmailAddress":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6f050e020a1c2f18060303061c410c0002">[email protected]</a>"}] I am lookin ...

What is the purpose of parsing my array if they appear identical in the console?

While working on a D3 component, I came across an interesting question. Why do different arrays require different data treatment even if they all contain the same information? In the first case, I have a 'hardcoded' array that looks like this: ...

VueJS refreshes components while preserving previous data

As a newcomer to VueJs, I am currently working with a Practice component that includes an ExerciseMC component. The parent component retrieves a question object (with a text property) from the backend through a request and passes it as a prop to the Exerci ...

Error Checking in AngularJS Form Submission

According to my form.json file, I have a form that needs validation and a simulated submission. Firstly, I need to address this issue: fnPtr is not a function Next, I want to submit the form to a mocked API endpoint that will return true or false. Can I ...

Having issues with the functionality of the Material UI checkbox component

Having issues with getting the basic checked/unchecked function to work in my react component using material UI checkbox components. Despite checking everything, it's still not functioning as expected. Can someone please assist? Here's the code s ...

The return value of a jQuery post request is consistently false

When I click on a tag, my code returns true but the data variable is false. Can you explain why this is happening? $('#AddProvince').click(function () { var url = '@Url.Action("SetProvinceList")'; var id = $('#Province&apo ...

Visuals failing to display following Angular project compilation

After completing the coding for my app, I attempted to put it into production mode and after testing, I noticed that the images were not displaying as the logo in the app. However, in development mode, everything seems to be working fine. This is the struc ...

What is the best way to convert my Chatbot component into a <script> tag for seamless integration into any website using React.js?

I have successfully integrated a Chatbot component into my Next.js application. https://i.stack.imgur.com/BxgWV.png Now, I want to make this component available for anyone to use on their own website by simply adding a tag. My initial approach was to cre ...

AG-GRID-ANGULAR - Is there a way to automatically check or uncheck a checkbox in the header component based on the status of checkboxes in the cell renderers?

Currently, I am using ag-grid for my project. In one of the columns, I have a checkbox in the header using headerComponentFramework and checkboxes in the corresponding cells using cellRendererFramework. My goal is to automatically check or uncheck the hea ...