Difficulty accessing properties in an object array

Aparently i dont understand how to access object arrays. can somebody explain to me why collection.pre & collection[pre] both return undefined?


function truthCheck(collection, pre) {
let filtered = collection.filter(a => a.pre === true) //Remove objects that dont contain the 'sex' property
console.log(collection.pre);  //Returns 'undefined'
return pre === filtered

}

truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex");

ive also tried this with similar results.

function truthCheck(collection, pre) {
for (let i = 0; i < collection.length; i++) {
let filtered = collection.filter(a => a[i][pre] === true)
}
return pre === filtered

}

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

Challenge: Everything Be True

Link to the challenge:

pre is a string that represents a key on the object elements of the collection array. So you need to use bracket notation to access it.

You’ve got other syntax/logic errors going on so I suggest you start back from the beginning with the hint above.

Before trying to solve the problem, first try logging the value at the [pre] key for each element in the array. Then you’ll know you’re accessing them correctly.

You should clear up for yourself what is an object, what is an array.

[
  {"user": "Tinky-Winky", "sex": "male"},
  {"user": "Dipsy", "sex": "male"}, 
  {"user": "Laa-Laa", "sex": "female"}, 
  {"user": "Po", "sex": "female"}
]

The first argument you pass to the thruthCheck function is an array consisting of objects. Arrays are ordered lists of elements, each element representing some data, which can be of various type(string, number, or object in the current case). Underneath arrays are also objects, as all data in JS, but this is irrelevant on the current subject.
Objects store data, which is stored within keys. Those keys dont have a particular order and data can be retrieved by calling the respective key. In your example, the objects which populate the collection array, consist of 2 keys(properties), user and sex.
To refer to one of the values in one of the objects, within the collection aray, you first need to call the array, then the index at which the respective object is positioned, and finally access the name of the key(prop) you look for. In the context of your example, collection[1]['sex'] would return the value of sex in the second object of the collection array, which is equal male.
The pre perimeter of the function you use wrong in several places of your code, you can simply look as a variable which value is equal what you passed to the function, in this case the value of sex. Within the function context, pre is equal to the string “sex”. The collection array has no property named pre, as arrays in general are not objects holding properties, like i alreayd explained, they are index based, so you refer to the values in their store with numbers. So collection.pre does not exist in the given context. Same goes for a.pre, which in practice you try to access an object in the collection array and call its key named pre, but there is no such key. Objects in that array only has the keys user and sex :slight_smile: . However, if you try to access a[pre](remember pre was equal “sex”), you would refer to the key named “sex”.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.