1

I have an js object that is stored to variable

var data = {
    "France":[
        {
            'population': "8M",
        }
    ],
    "Spain":[
        {
            'population': "18M",
        }
    ]
}

and I have an input field that have some value (choosed from dropdown).

<input type="text" id="country1" onkeyup="myFunction()" placeholder="Select indicator" value="France">

I am trying to compare that value of the input with key of the object, and then if that value is equal to the key, to display population number. So far I tried to do like this

$( ".compare button" ).click(function() {
        var c1 = $( ".first-country input" ).val(); //take value from input
        var zemlja = Object.keys(data); //take all keys from object
        var n = zemlja.includes(c1);   //check if value is included in object
        if (n) {
            $(".country1-result h1").html(data.c1[0].population);  //if value is included, add population number to result
        }  
    });

And I got undefined. I figure it out that I can not take value if I put variable c1 instead of name of the key. If I put name of the key France, everything works fine, but if I put c1 variable that is equal to France, then it is not working. Any hint/help how to manage this to work.

Thanks.

2
  • It would be nice if you can create a runnable snippet of your code. Faster to debug Commented Sep 5, 2018 at 13:40
  • You do not need this: var zemlja = Object.keys(data);. Instead you can just try .html(!!data[c1] && data[c1][0].population || '') Commented Sep 5, 2018 at 13:56

2 Answers 2

1

Since c1 is a variable, use bracket notation which will allow you to use property name as variable:

Change

$(".country1-result h1").html(data.c1[0].population);

To

$(".country1-result h1").html(data[c1][0].population);

var data = { "France": [{ 'population': "8M", }], "Spain": [{ 'population': "18M", }] }
function myFunction(){}

$(".compare").click(function() {
  var c1 = $(".first-country").val(); //take value from input
  var zemlja = Object.keys(data); //take all keys from object
  var n = zemlja.includes(c1); //check if value is included in object
  if (n) {
    $(".country1-result h1").html(data[c1][0].population); //if value is included, add population number to result
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="country1" onkeyup="myFunction()" placeholder="Select indicator" value="France" class="first-country">

<input type="button" class="compare" value="Click" />

<div class="country1-result">
  <h1></h1>
</div>

Sign up to request clarification or add additional context in comments.

5 Comments

Still not working, I got message Cannot read property of undefined.
you need to check if data[c1] exists. see Artem's answer
@MarkoPetković, I have updated the answer by adding some HTML and updating few selectors......please check.
It is working. I checked if data exists. Thanks to all.
@Mamun I have updated your code and added myFunction as it was throwing error. Yes, its a part of OP's code, but users will face this issue so added it. Please revert if the edit is not required.
0
$( ".compare button" ).click(function() {
        var c1 = $( ".first-country input" ).val(); //take value from input
        var result = data[c1] ? data[c1][0].population : '';
        $(".country1-result h1").html(result);
    });

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.