0

I am trying to create an object - that each parameter inside it is another object:

var divTextPerScreenWidthMap = new Object(
       {'360','click'},
       {'480','click it'},
       {'768','click it right'},
       {'1024','you know you want to click it'},
       {'1280','click this button which is very long will help you'}
    );

This is not working since I am getting an error. how do I need to write it to make it work? Should I change the outer object into an Array and how?

1
  • You have a syntactical error. Commented Feb 8, 2012 at 9:09

7 Answers 7

9

You have syntactical errors.

First of all object literal follows the syntax below:

var literal = {
    "Name": "value",
    "Array": [],
    "NestedObject": {}
};

Name value separator is the colon, not comma.

EDIT

The above code might be rewritten as follows

// declaration via array initializer
var myArray = [
   // name : value syntax
   {'360': 'click'},
   // values separated by comma
   {'480': 'click it'},
   {'768': 'click it right'},
   {'1024': 'you know you want to click it'},
   {'1280': 'click this button which is very long will help you'}
]

however at this point you cannot access your objects via i'ts names like this:

var firstObject = myArray[0];
// will throw an error
firstObject.360 = "not click";

You can only use it as follows

firstObject["360"] = "not click";

Hence I suggest you to name the properties according to variable naming rules.

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

Comments

1

In javascript object is a simple map. It is better to use literal {} instead od new Object();

  var myObj = {      
     prop : {},
     prop2 : {} 
   }

Comments

0

Don't create an Object via its constructor, use the literal syntax {}.

Also, objects have keys and properties. Your objects seem to only have values. Did you mean to use Arrays?

Comments

0

You completely forgot to give keys for your values. If you don't want to use keys, use arrays:

var divFoo = [
  [360, "click"],
  [480, "click it"] // et cetera
];

This would give you an array of arrays. For instance, divFoo[0][0] == 360

If you want keys, use an object:

var divFoo = {
  "360": "click",
  "480": "click" // et cetera
}

This gives you simple object. divFoo[360] == "click"

Or you could use an array of objects for more descriptiveness:

var divFoo = [
  {time: 360, text: "click"},
  {time: 480, text: "click it"} // et cetera
];

In this case, divFoo[1].text == "click it".

Also, a few hints:

  • Don't use new Object or new Array. They're redundant.
  • There's no need to quote integers if they're used as values.

Comments

0

It would make sense to represent your collection of objects as an array:

var divTextPerScreenWidthMap = [
           {360:'click'},
           {480:'click it'},
           {768:'click it right'},
           {1024:'you know you want to click it'},
           {1280:'click this button which is very long will help you'}
        ];

//You could iterate over your array of objects with a for loop:
        
      var i, value;
      for (i=0; i < divTextPerScreenWidthMap.length; i++) {
        value = divTextPerScreenWidthMap[i];
        console.log(value);
      };


//Alternatively, you could represent your data structure as one object:

    var divTextPerScreenWidthMap = {
           360:'click',
           480:'click it',
           768:'click it right',
           1024:'you know you want to click it',
           1280:'click this button which is very long will help you'
        };
    
//So now if you have a screen width, you can quickly get back the corresponding message:

    var screenWdith = 360;
    alert(divTextPerScreenWidthMap[screenWidth]);

Comments

0

You can also created nested objects like this:

let obj1 = {};
obj1['title'] = 'Vehicles';

let obj2 = {};
obj2['name'] = 'Honda City';

obj1['cars'] = obj2;

console.log(obj1);

Comments

0

Create a method in object, create a new object in that method and return it.

const obj = {
  one: "one is just a property",
  two: function(){
    const newObject = {
      three: "now two will return new a new object"
    }
    return newObject;
  }
}

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.