While experimenting with JavaScript, I attempted to retrieve an object nested within several other objects using the .find() method. It reminded me of how it can be done in MongoDB, where you can use the '.find()' method along with some hints to achieve similar results. However, my understanding is that the .find() method in JavaScript is primarily meant for arrays. Still, I was curious to see if it could work in the scenario I was testing...
Here is the code snippet:
const arrayLike = {
length: 1,
people: {
person: {
first name: 'rafa',
last name: 'rivas',
age: 20
},
person: {
first name: 'miguel',
last name: 'blades',
age: 23
},
person: {
first name: 'mario',
last name: 'perez',
age: 93
},
}
};
console.log(Array.prototype.find.call(arrayLike, (x) => x ));
After running this code, I found myself puzzled by the unexpected output. Instead of at least returning the array('people') as expected, the console displayed 'undefined'.