1

I have the following code:

            let self = this;
          
            for (const questionIndex in self.questions) {
                self.givenAnswers[questionIndex] = "";
            }

And PhpStorm is complaining about the line self.givenAnswers[questionIndex] = ""; saying that I need to check if the object has the property. The full message is:

Possible iteration over unexpected (custom / inherited) members, probably missing hasOwnProperty check.

But the givenAnswers variable is an array and not an object, and I want to append new key and values. How do I remove the warning, or is there anything wrong with the code?

0

1 Answer 1

1

While you could add a hasOwnProperty check to fix the warning:

for (const questionIndex in self.questions) {
    if (self.questions.hasOwnProperty(questionIndex)) {
        self.givenAnswers[questionIndex] = "";
    }
}

If it's an array, I think iterating from 0 to its length would be less verbose:

for (let i = 0; i < self.questions.length; i++) {
    self.givenAnswers[i] = "";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ok, the first one helped me to resolve the issue. It does seem kinda stupid for me on PHPStorm side - that if I am iterating over object keys - the object should have the key and why should it complain then. oO

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.