0

I am trying to access some data from an object as follows:

var summaryChanges = {
        dataToAdd:[
            {name:[]},
            {events:[]},
            {emails:[]}
        ],
        dataToRemove:[
            {name:[]},
            {events:[]},
            {emails:[]}
            ]
  }

i am trying to log the contents of the name property of data to add as follows:

console.log($(summaryChanges.dataToAdd.name)[0]);

however the console only logs undefined.

2
  • is that a valid object? Commented Jun 10, 2014 at 14:32
  • DataToAdd is an array, so you should access it as such. You can use dataToAdd[0] to do so. Commented Jun 10, 2014 at 14:38

2 Answers 2

3

dataToAdd is an arrary not an object , so access it like

console.log(summaryChanges.dataToAdd[0].name[0])
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks that fixed it, still getting used to syntax of objects etc, much appreciated
how would I format the object so as to be able to access checkpoints as a key (I read that js doesn't do associative arrays) ie summaryChanges.dataToAdd.name[i]
Figured it out,: var summaryChanges = { dataToAdd:{ checkpoints:[], events:[], emails:[] }, dataToRemove:{ checkpoints:[], events:[], emails:[] } }
0

You need to realize some things

  1. $(summaryChanges.dataToAdd.name) you are creating a jQuery Object.
  2. summaryChanges it's an object so you can do sumaryChanges.dataToAdd
  3. dataToAdd it's an array, so for get a value you access it like this dataToAdd[index]

At the end you access it like this

console.log(summaryChanges.dataToAdd[index].name[index])

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.