0

I'm declaring a multi dimensional array with key and values at jquery but its showing error here is my code:

var beadArray = 
 {
    0: {
    barCode: "843036905884",
    build: "144",
    height: "46",
    image: "https://www.brighton.com/charmbuilder/beads/v144_qijc0272.png",
    mask: [
            0: ["3", "36"],
            1: ["1", "38"],
            2: ["1", "38"],
            3: ["1", "38"],
            4: ["1", "37"],
            5: ["1", "37"],
            6: ["1", "37"],
            7: ["2", "37"],
            8: ["1", "38"]        
          ],
    name: "Autumn Spirit Bead",
    quantity: "484",
    rX: "19",
    rY: "23",
    retail: "29",
    style: "jc0272",
    theme: "A-E-H-I-J-L",
    type: "B",
    width: "40"
  }
};

its showing

Uncaught SyntaxError: Unexpected token : at 0: ["3", "36"], line

2 Answers 2

1

You cant define values with index in arrays

var arr = [0:"1"];

you have to set the value in an array like below

var arr = ["1"];

and you can access is as

arr[0];

So in your case your syntax should be

var beadArray = 
 [
{
barCode: "843036905884",
build: "144",
height: "46",
image: "https://www.brighton.com/charmbuilder/beads/v144_qijc0272.png",
mask: [
         ["3", "36"],
         ["1", "38"],
         ["1", "38"],
         ["1", "38"],
         ["1", "37"],
         ["1", "37"],
         ["1", "37"],
         ["2", "37"],
         ["1", "38"]        
      ],
name: "Autumn Spirit Bead",
quantity: "484",
rX: "19",
rY: "23",
retail: "29",
style: "jc0272",
theme: "A-E-H-I-J-L",
type: "B",
width: "40"
  }
];
Sign up to request clarification or add additional context in comments.

1 Comment

briliant its working for me thanks @soCalledDeveloper
1

Javascript does not have associative arrays like for example the array monstrosity in PHP. It has arrays and it has object literals which are similar to hashes or dictionaries in other languages.

Arrays should be used when you want a list of values:

[1,2,3,4]

Object literals are used to map a key to a value:

{ foo: 'bar' }

While you can use an integer as the key in an object literal its kind of pointless in this case as:

[
  ["3", "36"],
  ["1", "38"],
  ["1", "38"]
][1]

Will give the exact same result as:

{
    0: ["3", "36"],
    1: ["1", "38"],
    2: ["1", "38"]
}[1]

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.