Wherefore art thou - Object enumeration

Wherefore art thou - Object enumeration
0

#1

I had a rather general question here regarding this problem insofar as the proper way to think of Object ‘enumeration’ in Javascript.

From the Docs, if I have the terminology correct, ‘source’ has only one ‘property’, so it is easy to directly instantiate ‘Object’ to get at ‘key/value’ pairs. However, ‘collection’ has multiple ‘properties’. Knowing this ahead of time, it is easy enough to then adjust and pull these ‘key/value’ pairs from the respective [index].

In this problem, further, this configuration is already sort of ‘made known’ to us ahead of time. I guess what I am wondering is what if you didn’t know the number of properties/levels the incoming object had ? Or what if each individual property of the object also had a second layer contained within it ?

The ‘Object’ structure itself doesn’t seem to have a .depth or .length type function to be able to determine the size of the object you are dealing with.

What’s the right way to think/go about this ?

Your code so far

function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = Object.entries(collection);
  // Only change code below this line
  console.log(Object.entries(collection));
  
  // Only change code above this line
  return arr;
}

whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36.

Link to the challenge:


#2

collection is an array (a more specialize type of object in JavaScript). Arrays have indexes (properties) which are in a specific order (based on index value) by default, so that is how you know ahead of time how you will be able to access it. The collection array contains elements which are objects in this challenge. JavaScript objects just have properties and values and are not necessarily in a specific order based on the way they are structured.

Arrays in JavaScript are wrapped in [ ] and an array’s elements are separated by commas:

var myArray1 = [ 1, 2, 3, 4];
var myArray2 = ['a', 'b', 'c', 'd'];

Object literals in JavaScript have property/value pairs separated by commas while being wrapped in { }:

var myObject = {name: 'John', id: 23, phone: '408-555-1212'};

#3

Dear Randell,

Thanks again for clarifying that… I know you had already run through this with me before (thankfully getting close to the end of the exercises, so hopefully not too many more ‘?’).

You were very clear when you explained it the first time, so I took some moments to go back to some previous material, as well as re-examining the MDN.

I think I see it now. When I learned OOP for the first time it was with standard Java. In Java (or C variants), to have an object of some sort you have to have a ‘class’ and some related declaration of sorts. When we went through creating classes, at least as I was taught, we would add constructors and methods to allow for query and enumeration, such as when one would build other data structures such as a linked list, etc, so that one had a ‘complete’ data type/structure.

Where as ‘Objects’ in ‘javascript’, if I am now understanding right, out of the box, are just not like that-- more glorified arrays – though as you say based on key/value pairs and with special iterators attached to them.

Thanks for helping me get my mind around this…