0

I am trying to get load a variable from a array variable for my project but the value comes back as undefined I just used the console.log for the test output. I want to learn how to use it after seeing others use on their projects and I want to do it to make my projects easier to manage. I set it to trigger when the page loads up first thing.

Any help is welcome from the community.

My code if it helps solve it problem:

window.addEventListener('load',function(){
var values = [
odds_base = 10,
start_cash = 50
]
console.log(values.odds_base)
});

asked Jul 23, 2020 at 17:49
3
  • 1
    Are you sure values is array Commented Jul 23, 2020 at 17:52
  • 3
    var values ={ odds_base :10, start_cash : 50 } Commented Jul 23, 2020 at 17:53
  • 1
    if that is array then you should use this console.log(odds_base) Commented Jul 23, 2020 at 17:56

5 Answers 5

2

In this case I think you have to use an object like this one, instead of array:

window.addEventListener('load',function() {
 var values = {
 odds_base: 10,
 start_cash: 50
 };
 
 console.log(values.odds_base);
});
answered Jul 23, 2020 at 17:51
Sign up to request clarification or add additional context in comments.

3 Comments

it's not a JSON but just an object.
How do I get the data from the array @Murilo Góes de Almeida
@JoeMcMullan please be more specific (maybe ask a separate question?) what "get", "the data" and "the array" is
1

You are using the wrong data structure here. Square brackets are used for arrays. You should use a javascript object here.

window.addEventListener('load',function(){
 var values = {
 odds_base: 10,
 start_cash: 50
 }
 
 console.log(values.odds_base)
});
answered Jul 23, 2020 at 17:59

Comments

0

Or you can do like this

let values = {};
values.odds_base = 10;
values.start_cash = 50;
console.log(values.odds_base);

answered Jul 23, 2020 at 17:56

Comments

0

If that is array then you have to use variables like this

window.addEventListener('load',function(){
var values = [
odds_base = 10,
start_cash = 50
]
console.log(values);
console.log(odds_base);
console.log(start_cash);
});

When you assign array like this then Internally JavaScript first Creates the variable and then assign those variables in array

Otherwise convert that array to object and do like as follows

window.addEventListener('load',function(){
var values = {
odds_base : 10,
start_cash : 50
}
console.log(values);
console.log(values.odds_base);
console.log(values.start_cash);
});

answered Jul 23, 2020 at 17:58

Comments

0

The value of values looks like it should be an object and not an array as seen in your example i.e. values = [ a = 1, b = 2 ] should be values = { a: 1, b: 2 }

window.addEventListener('load', function(){
 var values = {
 odds_base: 10,
 start_cash: 50
 }
 console.log(values.odds_base)
});

Tip: Declare the values variable outside the scope of the addEventListener callback if you wish it to be accessible outside also.

answered Jul 23, 2020 at 18:04

2 Comments

Can I use values.odds_base with the example
If I understand the question correctly, then yes but only within the scope is has been declared in. You cannot use it outside of that load callback function unless you declare values outside.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.