Confused about 'Oject.keys()' and the meaning of 'enumerate properties'

Confused about 'Oject.keys()' and the meaning of 'enumerate properties'
0

#1

Hello. I’ve been doing some research online to understand how ‘Object.keys()’ works as well as to understand the meaning (and implication) of ‘enumerate properties’.

I still don’t quite understand them. Could somebody explain them to me? Thanks!


#2

If you haven’t already looked there, MDN is a great resource. Here is the Object.keys() page.
Basically, it gives you a list (array) of what properties an object contains.
Recall that a property consists of a key and a value.

const someObject = {
    key1: "some value",
    key2: "some other value"
};

“enumerating” basically means iterating over. Enumerating an objects properties is doing some variation on a for over them.


#3

var a = {key1: “value1”, key2: “value2”};
Object.keys(a) gives you array [“key1”, “key2”] so you know what you can get from the object like for instance a[Object.keys(a)[0]] is “value1”


#4

Thanks! I’m now looking at code written by a developer (http://www.mariadcampbell.com/2016/07/05/wherefore-art-thou/) and am confused about one line in particular: return obj.hasOwnProperty(key) && obj[key] === source[key]; Could someone explain what this line of code does? Thanks!

    "use strict";
    // What's in a name?
    var arr = [];
    // Only change code below this line
    // store keys present in source in variable keys
    var keys = Object.keys(source);
    // store collection argument in arr removing objects that don't contain
    // keys from source.
    arr = collection.filter(function(obj) {
        // use arr.every() method to check each collection key against keys from source.
        return keys.every(function(key) {
            // return the object(s) in collection that contain the key value pair(s)
            // missing in source that should be present there as well as the
            // key value pair(s) that are present in source.
            return obj.hasOwnProperty(key) && obj[key] === source[key];
        });
    });
 
    // Only change code above this line
    return arr;
}

#5

That particular line will return a value of true or false based on the overall evaluation of two conditions.
The first condition (below) will evaluate to true if obj has a property name equal to value of key.

obj.hasOwnProperty(key)

The second condition (below) will evaluate to true if obj’s key property is equal to the same value of source’s key property.

obj[key] === source[key]

Since there is an && operator between the two conditions, then both must be true for the returned value to be true. The every function returns a true or false value back to the filter if all the returns of the every callback function are true. If a single false is returned from this two condition statement you asked about, then the every function will return false, which will cause the filter function to not include the current obj being iterated over in the collection array.