如何添加到 Javascript 中的函数?

更改 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>


不负相思意
浏览 118回答 4
4回答

慕的地6264312

在返回之前,newArr您可以检查它的长度,如果它为零,则返回“No matches”。let people = [{&nbsp; &nbsp; name: "Nick",&nbsp; &nbsp; studying: "JS",&nbsp; &nbsp; age: "33"&nbsp; },&nbsp; {&nbsp; &nbsp; name: "Joseph",&nbsp; &nbsp; studying: "JS",&nbsp; &nbsp; age: "137"&nbsp; },&nbsp; {&nbsp; &nbsp; name: "Jeff",&nbsp; &nbsp; studying: "education",&nbsp; &nbsp; age: "1897374"&nbsp; }];function returnArrOfNames(people, string) {&nbsp; let newArr = []&nbsp; for (let i = 0; i < people.length; i++) {&nbsp; &nbsp; if (people[i].studying === string) {&nbsp; &nbsp; &nbsp; newArr.push(people[i].name)&nbsp; &nbsp; }&nbsp; }&nbsp; if (newArr.length === 0) {&nbsp; &nbsp; return "No matches";&nbsp; }&nbsp; return newArr}console.log(returnArrOfNames(people, "JS"));

有只小跳蛙

编辑:对不起,我发布了与其他人相同的答案。您可以只在 for 循环之外添加该 if 语句。像这样:let people = [&nbsp; {name : "Nick",&nbsp; studying : "JS",&nbsp; age : "33"},&nbsp; {name : "Joseph",&nbsp; studying : "JS",&nbsp; age : "137"},&nbsp; {name : "Jeff",&nbsp; studying : "education",&nbsp; age : "1897374"}];function returnArrOfNames(people, string) {&nbsp; &nbsp;let newArr = []&nbsp; &nbsp;for (let i = 0; i < people.length; i++) {&nbsp; &nbsp; &nbsp; if (people[i].studying === string) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newArr.push(people[i].name)&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;}&nbsp; &nbsp;if (newArr.length == 0){&nbsp; &nbsp; &nbsp; &nbsp;return console.log("no matches")&nbsp; &nbsp;}&nbsp; &nbsp;console.log(newArr);&nbsp; &nbsp;return newArr}returnArrOfNames(people, "JS")

白衣非少年

更简单的是array.reduce方法,使用三元条件返回const people =&nbsp;&nbsp; [ { name: 'Nick',&nbsp; &nbsp;studying: 'JS',&nbsp; &nbsp; &nbsp; &nbsp; age: '33'&nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; , { name: 'Joseph', studying: 'JS',&nbsp; &nbsp; &nbsp; &nbsp; age: '137'&nbsp; &nbsp; &nbsp;}&nbsp;&nbsp; , { name: 'Jeff',&nbsp; &nbsp;studying: 'education', age: '1897374' }&nbsp;&nbsp; ]&nbsp;function returnArrOfNames(arr, study)&nbsp; {&nbsp; let newArr = arr.reduce((a,c)=>&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; if (c.studying===study) a.push(c.name)&nbsp; &nbsp; &nbsp; return a&nbsp; &nbsp; &nbsp; }, [])&nbsp; return&nbsp; newArr.length ? newArr : 'no matches'&nbsp; }console.log(' JS ? ->', returnArrOfNames(people, "JS") )&nbsp; // Array [ "Nick", "Joseph" ]console.log(' xx ? ->', returnArrOfNames(people, "xx") )&nbsp; // "no matches".as-console-wrapper { max-height: 100% !important; top: 0; }

小唯快跑啊

您可以利用Array prototype's map和filter方法来获得结果:let people = [{&nbsp; &nbsp; name: "Nick",&nbsp; &nbsp; studying: "JS",&nbsp; &nbsp; age: "33"&nbsp; },&nbsp; {&nbsp; &nbsp; name: "Joseph",&nbsp; &nbsp; studying: "JS",&nbsp; &nbsp; age: "137"&nbsp; },&nbsp; {&nbsp; &nbsp; name: "Jeff",&nbsp; &nbsp; studying: "education",&nbsp; &nbsp; age: "1897374"&nbsp; }];function returnArrOfNames(people = [], studying) {&nbsp; const NO_MATCHES = 'No Matches'&nbsp; const isEmpty = people.length === 0&nbsp; if (isEmpty) {&nbsp; &nbsp; return NO_MATCHES&nbsp; }&nbsp;&nbsp;&nbsp; const filterFn = i => i.studying === studying&nbsp; const filteredPeople = people.filter(filterFn)&nbsp; const isEmptyFilteredPeople = filteredPeople.length === 0;&nbsp; if(isEmptyFilteredPeople) return NO_MATCHES&nbsp;&nbsp;&nbsp; const mapFn = i => i.name&nbsp; const mappedPeople = filteredPeople.map(mapFn);&nbsp; return mappedPeople}console.log(returnArrOfNames(people, "JS"));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript