第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Javascript 配置文件查找

Javascript 配置文件查找

ABOUTYOU 2023-03-18 17:43:57
我正在嘗試解決如下所示的以下 Javascript 問題。我一直在試圖找出它的哪一部分失敗了,并且已經失敗了一段時間。我確實有初始教程提供的解決方案,但不清楚為什么這不太有效。我們有一個代表我們聯(lián)系人列表中不同人的對象數組。已經為您預先編寫了一個以名稱和屬性 (prop) 作為參數的 lookUpProfile 函數。該函數應檢查 name 是否為實際聯(lián)系人的 firstName 以及給定屬性 (prop) 是否為該聯(lián)系人的屬性。如果兩者都為真,則返回該屬性的“值”。如果名稱不對應于任何聯(lián)系人,則返回“沒有這樣的聯(lián)系人”。如果 prop 不對應于找到的與名稱匹配的聯(lián)系人的任何有效屬性,則返回“沒有這樣的屬性”。// Setupvar contacts = [    {        "firstName": "Akira",        "lastName": "Laine",        "number": "0543236543",        "likes": ["Pizza", "Coding", "Brownie Points"]    },    {        "firstName": "Harry",        "lastName": "Potter",        "number": "0994372684",        "likes": ["Hogwarts", "Magic", "Hagrid"]    },    {        "firstName": "Sherlock",        "lastName": "Holmes",        "number": "0487345643",        "likes": ["Intriguing Cases", "Violin"]    },    {        "firstName": "Kristian",        "lastName": "Vos",        "number": "unknown",        "likes": ["JavaScript", "Gaming", "Foxes"]    }];function lookUpProfile(name, prop){// Only change code below this linefor (var i=0; i<contacts.length; i++){ for (var j=0; j<contacts[i].length; j++){if(contacts[i][0]===name&&contacts[i][j]===prop){ return contacts[i][j]    ;}else if (contacts[i]===name) {return "No such property";}else return "No such contact";}   }// Only change code above this line}lookUpProfile("Akira", "likes");
查看完整描述

2 回答

?
慕碼人8056858

TA貢獻1803條經驗 獲得超6個贊

我認為這是讓你感到困惑的 for 循環(huán)。問題要求我們檢查是否name does not correspond to any contacts then return "No such contact"。因此,在您的回答中,您一到達第一個無效聯(lián)系人就

返回,這不是想要的問題。 您應該將自己排除在 for 循環(huán)之外,作為最終的包羅萬象。else if (contacts[i]===name) {return "No such property";}

return "No such contact"


除此之外,當您需要檢查對象中是否存在某個屬性時,您可以使用Object.hasOwnProperty(propertyName). contacts[i]inelse if (contacts[i]===name) {return "No such property";}會給你聯(lián)系人中的實際對象,它永遠不會匹配name.

您將需要獲得name帶括號或點的注釋。例子:


contacts[0] is equal to

{

? ? "firstName": "Akira",

? ? "lastName": "Laine",

? ? "number": "0543236543",

? ? "likes": ["Pizza", "Coding", "Brownie Points"]

}

contacts[0].firstName is equal to "Akira"

contacts[0]["firstName"] is equal to "Akira"


// Setup

var contacts = [

? ? {

? ? ? ? "firstName": "Akira",

? ? ? ? "lastName": "Laine",

? ? ? ? "number": "0543236543",

? ? ? ? "likes": ["Pizza", "Coding", "Brownie Points"]

? ? },

? ? {

? ? ? ? "firstName": "Harry",

? ? ? ? "lastName": "Potter",

? ? ? ? "number": "0994372684",

? ? ? ? "likes": ["Hogwarts", "Magic", "Hagrid"]

? ? },

? ? {

? ? ? ? "firstName": "Sherlock",

? ? ? ? "lastName": "Holmes",

? ? ? ? "number": "0487345643",

? ? ? ? "likes": ["Intriguing Cases", "Violin"]

? ? },

? ? {

? ? ? ? "firstName": "Kristian",

? ? ? ? "lastName": "Vos",

? ? ? ? "number": "unknown",

? ? ? ? "likes": ["JavaScript", "Gaming", "Foxes"]

? ? }

];



function lookUpProfile(name, prop) {

? ? // Only change code below this line

? ? for (let i = 0; i < contacts.length; i++) {

? ? ? ? const contact = contacts[i];

? ? ? ? if (contact.firstName === name && contact.hasOwnProperty(prop)) {

? ? ? ? ? ? return contact[prop];

? ? ? ? } else if (contact.firstName === name && !contact.hasOwnProperty(prop)) {

? ? ? ? ? ? return "No such property";

? ? ? ? }

? ? }

? ? return "No such contact";

? ? // Only change code above this line

}


lookUpProfile("Akira", "likes");


查看完整回答
反對 回復 2023-03-18
?
MMTTMM

TA貢獻1869條經驗 獲得超4個贊

先找個人資料,再找房產


function lookUpProfile(name, prop) {

  const profile = contacts.find((contact) => contact.firstName === name)

  if (!profile) return "No such contact"


  const property = profile[prop]

  if (!property) return "No such property"


  return property

}

// Setup

var contacts = [

  {

    firstName: "Akira",

    lastName: "Laine",

    number: "0543236543",

    likes: ["Pizza", "Coding", "Brownie Points"],

  },

  {

    firstName: "Harry",

    lastName: "Potter",

    number: "0994372684",

    likes: ["Hogwarts", "Magic", "Hagrid"],

  },

  {

    firstName: "Sherlock",

    lastName: "Holmes",

    number: "0487345643",

    likes: ["Intriguing Cases", "Violin"],

  },

  {

    firstName: "Kristian",

    lastName: "Vos",

    number: "unknown",

    likes: ["JavaScript", "Gaming", "Foxes"],

  },

]


function lookUpProfile(name, prop) {

  // Only change code below this line

  const profile = contacts.find((contact) => contact.firstName === name)

  if (!profile) return "No such contact"


  const property = profile[prop]

  if (!property) return "No such property"


  return property

  // Only change code above this line

}


console.log(lookUpProfile("Akira", "likes"))

console.log(lookUpProfile("Bkira", "likes"))

console.log(lookUpProfile("Akira", "nikes"))


查看完整回答
反對 回復 2023-03-18
  • 2 回答
  • 0 關注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號