0

How can I retrieve an object child using other variable.

The Code

var product = {
  '123':{
    'name':'Basket',
    'price':'12.30'
  }
}

var id = 123;

var basket_price = product.id.price;    // I want to use the id value
                                        // as the reference to
                                        // retrieve the object child

How is the correct way to achieve that? Apparently the above way is unsuccessful because I don't specify any child with the key name of 'id' in the object definition.

2

1 Answer 1

0
var product = {
  '123':{
    'name':'Basket',
    'price':'12.30'
  }
}

var id = 123;

var basket_name = product[id].name; 
alert(basket_name);
var basket_price = product[id].name; 
alert(basket_price);
Sign up to request clarification or add additional context in comments.

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.