1

I see below code. And I just want to check if item.hasOwnProperty(key) is not helpful at all? My reason is that because we use for (let in), so we get every key in the object, and we do not need to check if this key is in the object. Am I right?

public convertObjectKeyToArray(item) {
                let array = [];
                for (let key in item) {
                    if (item.hasOwnProperty(key)) {
                        arr.push(key);
                    }
                }
                return array;
            }
user8565199
  • 133
  • 2
  • 8
  • every item in the array should have a key so it doesn't need to be checked for – clearshot66 Sep 08 '17 at 19:49
  • tl;dr: If you want to avoid odd behavior, keep the check. Alternatively, you could use a `for...of` loop. – Steven Goodman Sep 08 '17 at 19:53
  • Depends - is the item in the for loop the same as the item that is passed as param or is it a different array? – Jaya Sep 08 '17 at 19:55
  • **Own properties** are properties that are assigned to each instance of a class/object. **Not own properties** are properties that belong to the prototype chain: they are shared by all instances of the same class/object. – ibrahim mahrir Sep 08 '17 at 20:01
  • 1
    @Jaya Based on my code, it should be the same. Item in the for loop the same as the item that is passed as param – user8565199 Sep 08 '17 at 20:30
  • then i would agree with @clearshot66 there - it seems redundant and need not be checked for again – Jaya Sep 08 '17 at 22:34

0 Answers0