1

I have one problem statement.

Implement a function propertyExists(obj, path) that takes in an object and a path (string) as arguments and returns ‘False’ if the property doesn’t exist on that object or is null, else returns the value of the property.

And here is solution.

function propertyExists(obj,path) {
    // Write logic here
    let result =  obj.hasOwnProperty(path);
    if(result)
    {
     return (obj.path);       
    }
    else
    {
        return result;        
    }
}

Is this correct way of doing it?

4
  • Are you facing any type of issue with this implementation? Commented Nov 16, 2021 at 11:10
  • It depends on what the definition of path is, for me a path would be something like.. header.customer.address.lines[0] etc. But then your have called it propertyExists, so its likely property anyway, so not sure why its not propertyExists(obj, property) You likely want return obj[path] too. Commented Nov 16, 2021 at 11:11
  • The check seems incomplete based on requirements. You could be specific here. -> if (obj[path] === undefined || obj[path] === null) { return false; } else { return obj[path]; } Commented Nov 16, 2021 at 11:14
  • @SifatHaque Yes when I am submitting the solution I am getting wrong answer message Commented Nov 16, 2021 at 11:14

5 Answers 5

1

Multiple issues: the first name of the function should represent what it is doing, Path as variable name is vague but propertyName as a variable is clear. what you should do is either:

  1. write function called, "getValue" it returns value if exist or null

    function getValue(obj,propertyName) {
            if(!obj) { // if object not exist
           return null;
            }
           return  obj[propertyName];
        }

  1. write function called, "propertyExists" should return true if exist else false.

   function propertyExists(obj,propertyName) {
        if(!obj) { // if object not exist
       return false;
        }
       return  obj.hasOwnProperty(propertyName);
    }
 

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

Comments

1

function propertyExists(obj,path) {
    // Write logic here
    for(i in path){
        if(obj.hasOwnProperty(path[i]) === true){
            obj = obj[path[i]];
        }else{
            return false;
        }
    }
   return obj;
}

2 Comments

it may help to understand
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

object->obj,propertyname->path

function propertyExist(obj, path){
           if (obj.hasOwnProperty(path)) return obj[path];
                   else return false;
                      };

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

Here, as far as I can understand, the objects could be nested also.

Let's take obj as {"a":{"b":"dadsa"}} and path as ab

Now, the the solution which you have posted will not work. Ideally, it should return dadsa, but it will return false.

Try the below code, it will work for nested objects also.

function propertyExists(obj,path) {
    // Write logic here
    var val=obj;
    for(a of path){
        val=val[a];
        if(!val)
            return false;
    }
    return val;
}

Comments

0
function propertyExists(obj,path) {
    var val = obj;
    for (a of path) {
     val = val[a];
    if (!val){
      return false;
     }
     return val;
 }

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.