0

I have a problem.

I have the following JavaScript array created:

    <script type="text/javascript">
      ArrayName['FirstValue']['SecondValue'] = {
       name = 'checbox_id'
       checked = true }
    </script>

But when I call the Global Array in a function here is a sample call:

    ArrayName[value1][value2] = true;

I'm getting a messaging stating that ArrayName is undefined.

What would a correct way to create an array similar to the one above?

4
  • 4
    Well, where is ArrayName defined? Commented Dec 2, 2016 at 20:02
  • Let's start with, "Why do you want a global array?" Commented Dec 2, 2016 at 20:02
  • Cause I want to use it again... Commented Dec 2, 2016 at 20:03
  • 1
    There's no multi-dimensional arrays in JS. You can emulate them with nested arrays, but then you've to define the arrays one by one. Also, it looks like you'd need an object instead of an array? Commented Dec 2, 2016 at 20:03

1 Answer 1

2

You are attempting to set an element in an array called ArrayName, which is not declared anywhere:

<script type="text/javascript">
   // You are attempting to access an array but it hasn't been declared:
   ArrayName['FirstValue']['SecondValue'] = {
   name = 'checbox_id'
   checked = true }
</script>

Your syntax indicates that this is an array that contains nested arrays, like this:

var ArrayName = [[1,2,3], [4,5,6], [7,8,9]];

So, if you wanted to get the number 8, you'd need to access the third array element and get the second value found in the array stored there with this syntax:

// Remember, array indexes start counting from zero
ArrayName[2][1];  // Get the third element's, second item

Or (if we take your example literally, where you are searching for literal strings that are not positive integers), ArrayName wouldn't be an array, it would be an Object that you are trying to access the property FirstValue of and this property then stores another object as its value and that object has a property called SecondValue. That structure would look like this:

var myObjectLiteral = {
  FirstValue : { SecondValue : something }
}

But, without knowing what you are trying to accomplish, we can't possibly help you write the array or the object.

Next, we have to talk about the object that you are trying to assign to the array:

{
       name = 'checbox_id'
       checked = true
}

This syntax is incorrect. You are missing a comma to separate on key/value pair from the next. It should be:

{
       name : 'checbox_id',
       checked : true
}
Sign up to request clarification or add additional context in comments.

2 Comments

ArrayName = ['FirstValue']['SecondValue'] = {}; doesn't do what you think it does.
Since the code is actually: ArrayName['FirstValue']['SecondValue'] = { name = 'checbox_id' checked = true } I think it does.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.