-1

How does the function know how to iterate through all the keys in "obj"? Is this a feature already written in the objects element? In the if statement obj is iterating through all the elements in arr (currentElement =arr[i]). There is no loop is no loop to iterate through all the keys in obj. How is it able to do that without a loop?

(obj[currentElement] !== undefined)

 var arr = ['a', 'c', 'e'];
    var obj = {
      a: 1,
      b: 2,
      c: 3,
      d: 4
    };

    function select(arr, obj) {
      // initialize newObj to empty object; 
      var newObj = {};

      //create for loop to to iterate through current element in arr
      for (var i=0; i<arr.length; i++){
          var currentElement = arr[i];
          // if currentElement (key) exists in obj
          if (**obj[currentElement] !== undefined**){
              // currentElement  in newObj is equal to obj value 
              newObj[currentElement] =obj[currentElement];
          }
      }
      return newObj; 
    }

    var output = select(arr, obj);
    console.log(output); // --> { a: 1, c: 3 }

Thanks.

Nhan Bui
  • 157
  • 1
  • 1
  • 9
  • There is a loop. The keys of the object are manually provided in `arr` (some of them at lest). `for (var i=0; i – Felix Kling Apr 12 '20 at 20:17
  • "*There is no loop is no loop to iterate through all the keys in obj*" sure there is - `for (var i=0; i – VLAZ Apr 12 '20 at 20:18
  • If you are wondering why you can access an object property directly (`obj[currentElement]`) without having to iterate over all properties: That's a language feature and implementation detail. The language says that this needs to be possible and the implementation can do that however it wants (there might be some restrictions). Todays engines are heavily optimized and store object properties at known offsets in memory, kind of like structs in C. See https://v8.dev/blog/fast-properties for information on v8. – Felix Kling Apr 12 '20 at 20:22
  • @Flex King , in my mind the for loop allows the function to iterate through a,c, and e. SO obj[arr[i]] allows the function to compare obj keys to iterated array elements. For example, a ===a? true a===c?, false a===e?, false. Where is the loop to iterate to the next obj key is my question. – Nhan Bui Apr 12 '20 at 20:42

1 Answers1

0

It's not iterating through all the keys in the object. It's iterating through the elements in the array. It looks like it's iterating through the keys in the object because the array contains keys of the object. (obj[currentElement] !== undefined) is to make sure that the key in the array is also a key in the object.

Aplet123
  • 33,825
  • 1
  • 29
  • 55
  • the function has to check all the keys in the obj to make sure that elements in the array are also in the obj to come up with the correct result. if it is not iterating through the obj keys how does it check for b: 2, c: 3 , d: 4? – Nhan Bui Apr 12 '20 at 20:23
  • 1
    @NhanBui It does **not** check for `b` and `d` in `obj`, it only checks the keys `a`, `c` and the non-existent `e` – Lennholm Apr 12 '20 at 20:29
  • @NhanBui You can check if a key is in an object without having to go through all the keys in an object. `obj[currentElement]` will be `currentElement` is not in `obj`. – Aplet123 Apr 12 '20 at 20:36