Soy bastante nuevo en javascript y he estado luchando con este tipo de desafío. Entiendo el problema e implemento una lógica para resolverlo, pero no puedo mostrar los valores correctamente.
¿Alguien puede echar un vistazo a mi código y tal vez indicarme la dirección correcta?
const contacts = [ { name: "Laurel", phone: "123 456 7890", email: "laurel@comics.com", friends: ["Hardy", "Abbott", "Costello"], }, { name: "Hardy", phone: "321 654 0987", email: "hardy@hardyharhar.com", friends: ["Laurel", "Buster"], }, { name: "Buster", phone: "987 654 3210", email: "buster@keaton.ca", friends: ["Hardy"], }, { name: "Abbott", phone: "888 123 4567", email: "abbott@whosonfirst.co", friends: ["Costello", "Laurel"], }, { name: "Costello", phone: "767 676 7676", email: "costello@imonfirst.co", friends: ["Abbott", "Laurel"], }, ]; function findFriend(contacts, name, field) { let results = {}; contacts.forEach(function (elm) { // loop through all contacts and look for name if (elm.name === name) { // select the first friend let friend = elm.friends[0]; contacts.forEach((elm) => { // looking for the friend in the contacts object if (elm.name === friend) { // when found - this will get the required field and write it to the result results = elm[field]; } }); } }); return results; // return the results }/
/ Test cases console.log(findFriend(contacts, "Abbott", "phone")); // returns {name: "Costello", phone: "767 676 7676"} console.log(findFriend(contacts, "Buster", "email")); // returns {name: "Hardy", email: "hardy@hardyharhar.com"} console.log(findFriend(contacts, "Bob", "phone")); // returns "Not found" console.log(findFriend(contacts, "Costello", "birthday")); // returns "Not found"Deberías cambiar results = elm[field]; to if (elm[field]) { results[field] = elm[field]; results.name = elm.name; } , esto porque necesita verificar que la propiedad exista. También debe cambiar return results; por return Object.keys(results).length ? results : "Not found"; si los resultados están vacíos significa que el amigo no fue encontrado en la lista de contactos o que la propiedad buscada no está configurada en esa persona.
const contacts = [ { name: "Laurel", phone: "123 456 7890", email: "laurel@comics.com", friends: ["Hardy", "Abbott", "Costello"], }, { name: "Hardy", phone: "321 654 0987", email: "hardy@hardyharhar.com", friends: ["Laurel", "Buster"], }, { name: "Buster", phone: "987 654 3210", email: "buster@keaton.ca", friends: ["Hardy"], }, { name: "Abbott", phone: "888 123 4567", email: "abbott@whosonfirst.co", friends: ["Costello", "Laurel"], }, { name: "Costello", phone: "767 676 7676", email: "costello@imonfirst.co", friends: ["Abbott", "Laurel"], }, ]; function findFriend(contacts, name, field) { let results = {}; contacts.forEach(function (elm) { // loop through all contacts and look for name if (elm.name === name) { // select the first friend let friend = elm.friends[0]; contacts.forEach((elm) => { // looking for the friend in the contacts object if (elm.name === friend) { // when found - this will get the required field and write it to the result if (elm[field]) { results[field] = elm[field]; results.name = elm.name; } } }); } }); return Object.keys(results).length ? results : "Not found"; // return the results } console.log(findFriend(contacts, "Abbott", "phone")); // returns {name: "Costello", phone: "767 676 7676"} console.log(findFriend(contacts, "Buster", "email")); // returns {name: "Hardy", email: "hardy@hardyharhar.com"} console.log(findFriend(contacts, "Bob", "phone")); // returns "Not found" console.log(findFriend(contacts, "Costello", "birthday")); // returns "Not found"