0

I need to store information similar to this:

tag_array['design1']['0'][0]['x'] = 100;
tag_array['design1']['0'][0]['y'] = 100;
tag_array['design1']['0'][0]['w'] = 710;
tag_array['design1']['0'][0]['h'] = 332;

This would work if it is PHP but it's not working in javascript.

How do I create the same array in proper javascript syntax?

2
  • Refer this Post stackoverflow.com/questions/966225/… Commented Sep 5, 2012 at 18:22
  • 1
    well you would be better off with objects than arrays since you are using strings. Commented Sep 5, 2012 at 18:23

2 Answers 2

2

In PHP, you can do that because it will make the parent elements too (it actually throws a warning). In JavaScript, it will error out because the parent elements don't exist.

Instead of making it like that, try to make it all at once like this:

var tag_array = {
    design1: {
        '0': [{
            x: 100,
            y: 100,
            w: 710,
            h: 332
        }]
    }
};

This should give you the structure you want.

UPDATE: Answer has been updated to reflect the fact that the 1st 0 can be a string.

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

6 Comments

but it is a string "0" not an integer.
String versions of numeric properties can be used with numerically-indexed array elements, @epascarello, but you're right it's not clear which setup the OP wants.
OP here. Yes, the first "0" is a string. Can be replaced with any other strings.
In javascript, when you access a property it is accessed as a string. Since arrays are just fancy objects, they behave the same way. a[0] and a["0"] do the same thing because in the first case the number 0 is converted to a string before looking up the property.
@Rocket How do I directly insert into number 4 using your syntax? Like the following: tag_array['design1']['0'][4]
|
-1

If you want to do it programatically then because JavaScript requires you to have this type of structure

tag_array = {};
tag_array['design1'] = {};
tag_array['design1']['0'] = [];
tag_array['design1']['0'][0] = {};
tag_array['design1']['0'][0]['x'] = 100;

you can make a prototype function to create the object/array structure for you

Object.defineProperty(Object.prototype, 'addDimensions', { value: function(){
    var prop = arguments[0];
    if(this[prop] !== undefined){
        if(typeof this[prop] !== 'object') throw 'Property "'+prop+'" is already defined.'
    }else{
        if(arguments.length > 1 && ~~arguments[1] === arguments[1] && arguments[1] >= 0) this[prop] = [];
        else this[prop] = {};
    }
    if(arguments.length > 1){
        this[arguments[0]].addDimensions.apply(
            this[arguments[0]],
            Array.prototype.slice.call(arguments,1)
        );
    }
    return this;
},
enumerable: false });

and call it on an empty object {}

var myArrayLikeObject = ({}).addDimensions('design1','0',0); // {design1: [ [ {} ] ] }

or an array [] (only if first arg is an integer)

var myMultiArray = [].addDimensions(0,'hi',0); // [ {hi: [ {} ] } ]

or on an existing array or object

var a = [0,1,2];
a.addDimensions(3,0); // a = [0, 1, 2, [ {} ] ]

3 Comments

You'll need to modify it a little to check for existing sub-arrays / objects if you want to call it multiple times on the same indices
-1: a) Never add enumerable properties to Object.prototype, b) use objects ({}) for non-numeric properties, not arrays
@Bergi, does the edit I just made solve the issues you had with it?

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.