为了账号安全,请及时绑定邮箱和手机立即绑定

如何添加到 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 关注
  • 94 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信