0

How do I access something like this with Javascript and Jquery?

"condition" : [{"conditionId":["3000"],"conditionDisplayName":["Used"]}]

(this is just an excerpt)

I tried using item.condition and I got:
[{"conditionId":["3000"],"conditionDisplayName":["Used"]}] as a result.

How do I get the conditionDisplayName?

I've also tried using : item.condition.conditionDisplayName but it doesn't work nor does item.condition[1]

2
  • 1
    Try item.condition[0].conditionDisplayName Commented Jul 11, 2013 at 16:00
  • item.condition[1] - close, but array indices in javascript start at 0 not 1 Commented Jul 11, 2013 at 17:42

5 Answers 5

2

You're almost there but your condition is an array with one object so you need to use the array notation to access it:

var displayName = item.condition[0].conditionDisplayName;

If there could be more than one object in the array, you can use a loop:

for(var i=0; i<item.condition.length; i++) {
    console.log( item.condition[i].conditionDisplayName );
}
Sign up to request clarification or add additional context in comments.

Comments

2

condition is an array, as is conditionDisplayName, so you will need to access their members using an index. For example, to get the first displayName of the first condition you would do:

var displayName = item.condition[0].conditionDisplayName[0]

Comments

1

You can use array index to get the object

var displayName = obj.condition[0].conditionDisplayName;

Comments

1

In your case you should try:

item.condition[0].conditionDisplayName

Comments

1
var array = [{"conditionId":["3000"],"conditionDisplayName":["Used"]}]

// Not right - show ["Used"]
console.log(array[0].conditionDisplayName);

// Right - show "Used"
console.log(array[0].conditionDisplayName[0]);

You can see this here: http://jsfiddle.net/bs9kx/

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.