0

Here's my code:

var Constants = {
    strings = {
        FIRST: 'First Value',
        SECOND: 'Second Value',
        THIRD: 'Third Value',
    },
    numbers = {
        FIRST: 1,
        SECOND: 2,
        THIRD: 3,
    }
};

And this how I need to call the array:

Constants.strings.FIRST
Constants.numbers.FIRST

But I got this error: "SyntaxError: missing: after property id"

3
  • 1
    swap your = signs for : Commented Jun 6, 2020 at 20:13
  • 1
    also, they're not arrays - they're objects. See dev.to/zac_heisey/objects-vs-arrays-2g0e Commented Jun 6, 2020 at 20:15
  • 1
    you used = instead of a colon (:) Commented Jun 6, 2020 at 20:34

2 Answers 2

1

This is actually not an array, it is a nested object of objects. So each nested object have a pair of key and value.

let's say we got strings as key and

{
  FIRST: 'First Value',
  SECOND: 'Second Value',
  THIRD: 'Third Value',
}

as value (in this particular case, the value itself is an object too), so each key and value have to be separated by a colon.

Then, each pair should look like the below example instead of provided one:

strings: {
  FIRST: 'First Value',
  SECOND: 'Second Value',
  THIRD: 'Third Value',
}

You are getting an error in this case because you used = instead of a colon (:) and it won't be recognized as an object.

So your final object should be something like this:

var Constants = {
    strings: {
        FIRST: 'First Value',
        SECOND: 'Second Value',
        THIRD: 'Third Value',
    },
    numbers: {
        FIRST: 1,
        SECOND: 2,
        THIRD: 3,
    }
};

console.log(Constants.strings.FIRST)
console.log(Constants.numbers.FIRST)

Sign up to request clarification or add additional context in comments.

Comments

1

Change it to this

var Constants = {
    strings : {
        FIRST: 'First Value',
        SECOND: 'Second Value',
        THIRD: 'Third Value',
    },
    numbers : {
        FIRST: 1,
        SECOND: 2,
        THIRD: 3,
    }
};

Comments

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.