亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

如何添加到 Javascript 中的函數?

如何添加到 Javascript 中的函數?

不負相思意 2023-04-20 16:21:56
更改 removeproduct 代碼如下。并將 product-id 作為參數傳遞給函數。const removeProduct = (productId) => {    let cart = JSON.parse(localStorage.getItem("cartProduct"));    cart = cart.filter(productData => productData._id !== productId)    localStorage.setItem("cartProduct", JSON.stringify(cart));    window.location.reload(); };有關filter方法的更多詳細信息,請參閱 MDN 文檔。如下更改您的組件以將 id 參數傳遞給函數。<Container>      <div>      {        cart.map(item => (         <p>item.productName</p>         <p>item.price</p>         <i          class="fas fa-trash-alt mr-1"          style={{ color: '#ff6b6b' }}          onClick={() => removeProduct(item._id)}         ></i>        ))      }      </div></Container>
查看完整描述

4 回答

?
慕的地6264312

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

在返回之前,newArr您可以檢查它的長度,如果它為零,則返回“No matches”。


let people = [{

    name: "Nick",

    studying: "JS",

    age: "33"

  },


  {

    name: "Joseph",

    studying: "JS",

    age: "137"

  },


  {

    name: "Jeff",

    studying: "education",

    age: "1897374"

  }

];


function returnArrOfNames(people, string) {

  let newArr = []

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

    if (people[i].studying === string) {

      newArr.push(people[i].name)

    }

  }


  if (newArr.length === 0) {

    return "No matches";

  }


  return newArr

}


console.log(returnArrOfNames(people, "JS"));


查看完整回答
反對 回復 2023-04-20
?
有只小跳蛙

TA貢獻1824條經驗 獲得超8個贊

編輯:對不起,我發布了與其他人相同的答案。


您可以只在 for 循環之外添加該 if 語句。像這樣:


let people = [

  {name : "Nick",

  studying : "JS",

  age : "33"},


  {name : "Joseph",

  studying : "JS",

  age : "137"},


  {name : "Jeff",

  studying : "education",

  age : "1897374"}

];


function returnArrOfNames(people, string) {

   let newArr = []

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

      if (people[i].studying === string) {

          newArr.push(people[i].name)

      }

   }

   if (newArr.length == 0){

       return console.log("no matches")

   }


   console.log(newArr);

   return newArr

}



returnArrOfNames(people, "JS")


查看完整回答
反對 回復 2023-04-20
?
白衣非少年

TA貢獻1155條經驗 獲得超0個贊

更簡單的是array.reduce方法,使用三元條件返回


const people = 

  [ { name: 'Nick',   studying: 'JS',        age: '33'      } 

  , { name: 'Joseph', studying: 'JS',        age: '137'     } 

  , { name: 'Jeff',   studying: 'education', age: '1897374' } 

  ] 

function returnArrOfNames(arr, study)

  {

  let newArr = arr.reduce((a,c)=>

      {

      if (c.studying===study) a.push(c.name)

      return a

      }, [])

  return  newArr.length ? newArr : 'no matches'

  }

console.log(' JS ? ->', returnArrOfNames(people, "JS") )  // Array [ "Nick", "Joseph" ]

console.log(' xx ? ->', returnArrOfNames(people, "xx") )  // "no matches"

.as-console-wrapper { max-height: 100% !important; top: 0; }


查看完整回答
反對 回復 2023-04-20
?
小唯快跑啊

TA貢獻1863條經驗 獲得超2個贊

您可以利用Array prototype's map和filter方法來獲得結果:


let people = [{

    name: "Nick",

    studying: "JS",

    age: "33"

  },


  {

    name: "Joseph",

    studying: "JS",

    age: "137"

  },


  {

    name: "Jeff",

    studying: "education",

    age: "1897374"

  }

];



function returnArrOfNames(people = [], studying) {

  const NO_MATCHES = 'No Matches'

  const isEmpty = people.length === 0

  if (isEmpty) {

    return NO_MATCHES

  }

  

  const filterFn = i => i.studying === studying

  const filteredPeople = people.filter(filterFn)

  const isEmptyFilteredPeople = filteredPeople.length === 0;

  if(isEmptyFilteredPeople) return NO_MATCHES

  

  const mapFn = i => i.name

  const mappedPeople = filteredPeople.map(mapFn);

  return mappedPeople

}


console.log(returnArrOfNames(people, "JS"));


查看完整回答
反對 回復 2023-04-20
  • 4 回答
  • 0 關注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

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

公眾號

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