1

Hello All^^ I apologize for the simplicity of this question (and my JS comprehension), beforehand. Could someone explain why the following code returns the key value in the object array? I would expect that array-name[n] would return the key, rather than the value in the array. I thought that array-name[n][1] would produce the value (name of the shape in this example) in this function ( i.e., if 4 is entered, "square" would be returned). For the background, this was an edabit challenge that I solved. However, that was not entirely on purpose(I was initially trying to use a loop to perform the same function for about a half hour, to no avail). To digress, I would simply like to understand this more thoroughly, if possible. I appreciate your time.

    const polygons = {
     1 :  "circle" ,
     2 :  "semi-circle",
     3 :  "triangle",
     4 :  "square",
     5 :  "pentagon",
     6 :  "hexagon",
     7 :  "heptagon",
     8 :  "octagon",
     9 :  "nonagon",
     10 : "decagon"
};
return polygons[n];
}
3
  • What is n in your example? a number between 1 and 10? Commented Feb 5, 2020 at 20:46
  • Because that's how objects (polygons isn't an array here) work. If you want the keys use let keys = Object.keys(polygons); Commented Feb 5, 2020 at 20:50
  • Yes, n is always a number between 1 and 10^^ Sry for leaving that bit out. Commented Feb 5, 2020 at 20:50

1 Answer 1

4

Your polygon is an object. It is not an array hence you are seeing this behavior. See code below for better explanation.

// This is object. Doing polygons[4] will give you square
const polygons = {
  1 :  "circle" ,
  2 :  "semi-circle",
  3 :  "triangle",
  4 :  "square",
  5 :  "pentagon",
  6 :  "hexagon",
  7 :  "heptagon",
  8 :  "octagon",
  9 :  "nonagon",
  10 : "decagon"
};

function getFromObject(n) {
  return polygons[n];
}

// This is array. Doing polygons[4] will give you pentagon (because array index start from 0)
const polygons = [
  "circle" ,
  "semi-circle",
  "triangle",
  "square",
  "pentagon",
  "hexagon",
  "heptagon",
  "octagon",
  "nonagon",
  "decagon"
];

function getFromArray(n) {
  return polygons[n];
}

If you need keys from your object, you could do Object.keys(polygons). This return you array of keys. I am not sure what your usecase is but this how it works.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the explanation. I thought that this was called an "object array", rather than a regular array. However, my coding verbiage understanding is poor. So 1 : "circle" is simply one value of the object? They are still key and values, are they not? If I am mistaken (which is likely the case), then what would 1 and "circle" be called, respectively? Please let me know.
1: "circle" is a key/value pair in the object polygons. 1 is the key which corresponds to the value "circle". Together, they create a key/value pair, which is unique; you cannot have 2 identical keys in an object. MDN Object Docs is a great resource to learn more!
Thank you for the explanation. That makes sense and confirms my understanding of key/values. I suppose, what was confusing to me was why object[n] grabs only the value. I thought that a one bracketed number argument would translate to the key position (rather than the value position). Anyway, thanks again^^ Really appreciate it.

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.