What is the proper way to refer to an array or object within a function in JavaScript

Greetings! I am facing an issue with a JavaScript array declared outside of a function as shown below:

var daily = [];
daily["data"]=[];
daily["data"].push('hello');

function demo()
{
console.log(daily); // not working here
}

Can anyone advise on how to declare this object as global in JavaScript?

Answer №1

One possible reason for the issue could be due to hoisting of your function. To resolve this, you can try the following approach for defining your function.

var example = function(){
  console.log(daily);
}

Alternatively, you may also consider passing the 'daily' variable as a parameter to your function like this:

var example = function(d){
  console.log(d);
}

Then, when you need to use it, simply call it like so:

example(daily);

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

Utilizing Jquery's each() method to retrieve the exact position of a specific class element

Is there a way to determine the position of the "owl-item active" element? The current position is 2 out of 3 total photos. <div style="transform: translate3d(-825px, 0px, 0px); transition: all 0.25s ease 0s; width: 48675px;" class="owl-stage"> ...

Tips for altering the class of an HTML button dynamically when it's clicked during runtime

I currently have a set of buttons in my HTML page: <button id="button1" onclick="myFunction()" class="buttonClassA"></button> <button id="button2" onclick="myFunction()" class="buttonClassA"></button> <button id="button3" onclic ...

How to Update a Nested Document in Mongoose

I am currently working on a polls application using angular, express, and mongoose. Here is an overview of my data structure: var mongoose = require('mongoose'); var responseSchema = new mongoose.Schema({ responseText: String, votes: { ...

What is the method for retrieving a value with a designated key?

An array has been created from JSON source using the $.each method. The console displays the following: $vm0.sailNames; [Array(24), __ob__: Observer] (Interestingly, jQuery seems to have created an Observer) In the Vue.js development view, it appears li ...

In C programming, when a char array is truncated, it will automatically be assigned to the next char

I'm having an issue with inputting two char arrays from the user and truncating the characters if they exceed a specified length. Here's what I've tried so far: int main(){ printf("Enter Password: "); char password[9]= {0}; fge ...

Access the current page's URL using JavaScript

I have a unique URL structure that looks like this: index.html#page:page.php As I am loading my pages dynamically using AJAX, I want to create hotlinks to load specific pages. Currently, I am using the following function: function getdata(file){ $ ...

Deactivate the Mention and Hash tag in ngx-linkifyjs

I am currently utilizing ngx-linkifyjs to automatically convert URLs in text to clickable hyperlinks. However, I am facing an issue where it is also converting # and @ tags into links. Is there a way to prevent the conversion of # and @ while maintain ...

The onDrop event in javascript seems to be failing to execute

My attempts to get the javascript onDrop event to execute when an object is dropped into a drop zone have been unsuccessful. I've tried rewriting it in various ways without any success or error messages. In my search for potential reasons why the ondr ...

Tips for successfully sending a variable through setOnClickListener

Overview: The main layout, map_midvalley_g, contains 8 image buttons that can be clicked. Using a for loop on the data retrieved from the server, the color (green or red) of the image buttons is set. Clicking on any of the image buttons will trigger a popu ...

Navigate through the components of an array within HTML

I am a beginner in HTML and I need to customize a specific piece of code to suit my requirements. Here is the code snippet written in pseudo code: <app-myapp *ngIf="true" [exclude] = "[this.myUser.id]" ...

Looking for a way to extract Regular Expressions from an IgGrid cell in Infragistics?

Is it possible to apply a regular expression to a igTextEditor within an igGrid Updating? I attempted to utilize the validate option, but it was unsuccessful. $("#schedulerTable").igGrid({ columns: $scope.schedulerColumns, widt ...

Hiding the filelist on antd-Upload when the URL is not provided

When passing a Url as a prop to display uploaded files in an edit Modal, I want to ensure that no attachments are shown if the Url is empty. Currently, I am still seeing empty attachments displayed as shown in the image: https://i.sstatic.net/dkzu1.png ex ...

"jQuery Hide-and-Seek: A Fun Way to Manip

I am facing a challenge with a set of divs that I need to dynamically show and hide based on user interactions with questions. Essentially, the user will be presented with a question. Based on their answer, specific content with a title and description wi ...

jQuery Multi-select - Event Handler for Selecting All Items

I am currently using a jQuery multiselect that has the select all option checked by default. When there is a change in the selected options, I reload the page data accordingly. Everything is working well except for the fact that clicking the 'Select ...

"Create a new row in the list by selecting an option from the drop-down

I'm experimenting with the following scenario. There is a function that reveals a hidden list based on a dropdown selection. To see it in action, please click here. What I am aiming to achieve is for Option1 to display the content of #List-Option1 ...

Trigger the submission of a form on the current php page when a checkbox is altered

Looking for assistance with the following code: <form id="form" method="post" action=""> <input type="checkbox" name="checkbox" onchange="$('#form').submit();"> <label class="onoffswitch-label" for="myonoffswitch"> <span ...

The request is unable to process the URL retrieved from the response body

Having trouble with the request not working properly with the URL extracted from the response body. Although the URL displays correctly in the console, using it directly just doesn't seem to work. request.post(`https://onesignal.com/api/v1/players/csv ...

Sync user information when alterations are made on a different device

As I create a Discord clone using Next.js, I've encountered an issue where when a server is deleted, another client can still see and use the server until the page is reloaded. When testing out the official Discord web app, changes seemed to happen in ...

Is it possible for you to pinpoint the mistake in the code below?

I've been trying to locate an error in the function but have been unable to do so. As a beginner in this area, I would appreciate your patience. <html> <head><title>print names</title> <script langua ...

The Next.js client-side JavaScript application experiences unforeseen disruptions without generating any console errors

My server is responsible for constructing HTML from a JSON response: { content: "<div id=\"_next\">...</div>...<script src=\"...\"></script>" } This is how my Node server handles the data: const output = "&l ...