0

I am trying to check, if certain words exist, but as far as I have tried, it seems not to be working.

 Chars = {
    ae: 'hello',
    oe: 'world',
};

if(ae in Chars){
    document.write('yes');
}else{
    document.write('no');
}   

I am just trying to know, if ae exists

1
  • Note that your code doesn't use JSON - what you have is an object created from an object literal. (JSON is a string format used for data interchange.) Commented May 12, 2013 at 21:45

4 Answers 4

3

Try this:-

object.hasOwnProperty

if(Chars.hasOwnProperty('ae'))
{
//Do something
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can just do

if(Chars.ae){...}
else {...}

6 Comments

But, I have over 100 ae like values to check, is there anyway this could be done with loops, instead of 100 if/elseif or switch() ? thanks btw
@Kuntilak An object can't have multiple properties of the same name. The last one will overwrite all others.
I tried your code now, it does not work, can you double check?
@Kuntilak I don't know how you are using it, but it works here.
Note that this works only if you know in advance that the value associated with ae can't be an empty string (or other falsy value).
|
0

If it's a single value that you know at coding time, you can do

if (Chars.ae !== undefined) {
    document.write('yes');
}
else {
    document.write('no');
}

If you want to be able to figure these out dynamically at runtime, like say you have a variable representing the property to check, then you can use the bracket notation.

Chars = {
    ae: 'hello',
    oe: 'world',
    .. bunch of other properties
};

function doesCharEntryExist(entry) {
    return Chars[entry] !== undefined;
}

console.log(doesCharEntryExist('ae'));
console.log(doesCharEntryExist('oe'));
console.log(doesCharEntryExist('blah'));

outputs

true
true
false

2 Comments

Note that if (Chars.ae) { works only if you know in advance that the associated value can't be an empty string (or other falsy value).
I meant to check for undefined, which I did in the second part. Updated the first one. Thanks for the hint!
0

To use the in operator you need to put ae in quotes:

if ("ae" in Chars){

Or you can use a variable as follows:

var valueToTest = "ae";
if (valueToTest in Chars) {

You said in a comment under another answer that you have over one hundred values to check. You don't say how you are managing those hundred, but assuming they're in an array you can use a loop:

var keyNamesToTest = ["ae", "xy", "zz", "oe"];
for (var i = 0; i < keyNamesToTest.length; i++) {
    if (keyNamesToTest[i] in Chars){
        document.write('yes');
        // key name exists - to get the value use Chars[keyNamesToTest[i]]
    }else{
        document.write('no');
    }
}

For the Chars object you showed with the test array I introduced you'd get a yes, two nos, and another yes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.