0

I have the following object

     {
     "timetable": {
         "MONDAY": {
             "start-end0": {},
             "start-end1": {},
             "start-end2": {},
             "start-end3": {},
             "start-end4": {}
         }
     }

i need to add "start-end5" to MONDAY. I tried to use dot operator to monday like timetable.monday.start-end5={} it says monday undefined

4 Answers 4

8
  • monday is not MONDAY
  • Since start-end4 is not a valid identifier, obj.timetable.MONDAY.start-end5 = {} will not compile; you need to use the bracket syntax.

Thus,

obj.timetable.MONDAY["start-end5"] = {};
Sign up to request clarification or add additional context in comments.

7 Comments

how can i add dynamically start-end5 start-end6... to my map??Is that possible?
@masSdev, what do you mean by dynamically? in iteration?
yes using iteration like obj.timetable.MONDAY["start-end"+count]
@masSdev, you can pass your value in the square brackets in your iteration. I think it should work.
@masSdev: What do you mean, "like obj.timetable.MONDAY["start-end"+count]"? That's exactly what would work, so... not sure what kind of answer you're expecting. However, unless the format is set by someone beyond your control, that's a bad way to do things; a better would be for to have obj.timetable.MONDAY.startEnd as an array of objects, so you'd access each using obj.timetable.MONDAY.startEnd[5]. You can create it e.g. using Array.apply(null, {length: 5}).map(i => ({})) (in ES6).
|
3

Variable name should be followed this restrictions

  • Blanks & Commas are not allowed.
  • No Special Symbols other than underscore(_) are allowed.
  • First Character should be alphabet or Underscore.

Try like this

var time = {
  "timetable": {
    "MONDAY": {
      "start-end0": {},
      "start-end1": {},
      "start-end2": {},
      "start-end3": {},
      "start-end4": {}
    }
  }
}

time.timetable.MONDAY["start-end5"] = {}

DEMO

Addtion:

how can i add dynamically start-end5 start-end6... to my map??Is that possible?

Ans

Add a loop and concat string according to value.

Like this

var time = {
  "timetable": {
    "MONDAY": {
      "start-end0": {},
      "start-end1": {},
      "start-end2": {},
      "start-end3": {},
      "start-end4": {}
    }
  }
}
for(var i=0;i<5;i++) // set the limit of loop according to your need
  time.timetable.MONDAY["start-end"+i] = {}

DEMO

7 Comments

well, that's the most basic restrictions @Rayon
how can i add dynamically start-end5 start-end6... to my map??Is that possible?
I have added my ans @masSdev
its giving monday as undefined
check demo i have attached @masSdev
|
2

You need to use the [""] notation here, as your key name is not camelCase or other valid object key naming

a.timetable.MONDAY["start-end5"] = {};

Comments

1

One more completely viable (and very readable) syntax I'd like to add, just for fun:

time["timetable"]["MONDAY"]["start-end5"] = {};

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.