views:

125

answers:

2

I have an array comprising records

v = [{stringA, stringB, arr[{stringC,stringD}]

When I try to extract the value of stringA and stringB, Javascript returns {Object, Object}

I am trying to use

strX = v[4].arr[2].stringC;

(this approach works when extracting stringA and stringB, but not when extracting stringC)

Please, does anyone know how this should be done?

+1  A: 

Your syntax is badly inaccurate. It's quite difficult to make out what structure you're trying to achieve, but this won't even execute, because of unmatching numbers of [, ] and {, } in your declaration of v.

  • You can create an array by specifying comma-separated items within [,]
  • You can create an object by specifying comma-separated property:value pairs within {, }

If you want to be able to write v[4].arr[2].stringC then you need a structure that looks like

var v = [item0, item1, item2, item3, {
      description: 'this is item 4',
      arr: [ subitem0, subitem1, {
          description: 'this is arr[2]',
          stringC: 'this is the value of string c'
      }
  }];
David Hedlund
A: 

You want to use:

strX = v[0].arr[0].stringC;

I see no point for you putting those objects into those arrays. If you remove the array square brackets, the solution you will want to use is:

strX = v.arr.stringC;
Eli Grey