0

Say I have an array :

var courseArray = array('0.15' => '10 Unités', '0.20' => '20 Unités', '0.30' => '30 Unités');

Now I want to to corresponding value from this array if I know the key.

Something like will do ?

var courseArray = array('0.15' => '10 Unités', '0.20' => '20 Unités', '0.30' => '30 Unités');
var courseKey = $( "#amount" ).val();
var courseValue = courseArray[courseKey];

Thanks for any help.

2
  • Is that a valid array? Google Objects in Javascript Commented Oct 19, 2015 at 10:37
  • 1
    var courseArray = {'0.15':'10 Unités', '0.20' :'20 Unités', '0.30':'30 Unités'}; Commented Oct 19, 2015 at 10:39

1 Answer 1

1

use below code .

Array you have created is not valid;

you need to create JSON Object to get data base on key . (LINK)

DEMO

var courseArray = {'0.15':'10 Unités', '0.20':'20 Unités', '0.30':'30 Unités'};
alert(courseArray['0.15']);
Sign up to request clarification or add additional context in comments.

1 Comment

@Jeremy Happy to help you :)

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.