簡體   English   中英

如何讓這個 function 為第一個之后的每個項目返回一個新數組以及字符串“在購物車中找不到商品”?

[英]How do I get this function to return a new array for every item past the first one and the string "No items found in cart"?

所以,我是 JavaScript 的新手,我無法讓 function 按我想要的方式工作。 就目前而言,它可以很好地打印“番茄”的內容,並且可以判斷一個項目是否不在列表中......有點。 如果我輸入“t-shirt”或“soap”,它不會將其內容添加到 newArray 變量中,而是添加“Item not found”。 所以,它不識別任何過去的“番茄”。 另一個問題是它不會告訴我數組是否不為空,因此它永遠不會顯示“在購物車中找不到任何物品”,只是一個空數組。 這是我到目前為止所嘗試的。 任何幫助,將不勝感激。 謝謝你。

let items =  
    [
      {
        name: "tomato",
        type: "produce",
        price: 0.99
      }, 
      
      {
        name: "soap",
        type: "cleaning",
        price: 0.99
      },
      
      {
        name: "t-shirt",
        type: "clothing",
        price: 5.99
      }
    ]
    
    function searchItem(items,string)
    {
      let newArray = []; 
      for (i=0;i<items.length;i++)
      { 
          if (string === items[i].name)
          {
            newArray.push(items[i])
            break
          }
            else if(string != items[i].name)
            {
              newArray.push("Item not found")
              break
            }
             else
            {
              return "Item does not exist"
            }
      }
      return newArray
    }
    
    searchItem(items,"tomato")

這是searchItem function 中處理的邏輯的問題。

您應該僅在循環執行完成后設置"Item not found" ,否則如果數組中的第一項與搜索元素不匹配,它將返回"Item not found"而不檢查其下方的項目。

在您的代碼中,您正在遍歷items數組並檢查items數組中每個項目的name 如果名稱與 function 的參數匹配,則將其推送到 output 陣列並破壞 function。 如果您只想找到具有該值的第一個元素,則可以在此處添加中斷。 如果您想找到該字符串的所有組合,您可以避免在該行出現中斷(我采用了這種方法)。

問題出現在else if部分。 如果此數組中的項目name與字符串不匹配,則它將"Item not found"推送到newArray並中斷循環。 因此,這將中斷數組中的第一個不匹配元素並停止執行進一步的循環。 因此,如果搜索元素不在第一個索引處,function 將停止執行並將"Item not found"推送到"newArray"新數組”並中斷。 Rest 的代碼塊永遠不會執行。

要使您的 function 正常工作,請將else-ifelse移出循環。 讓循環完成執行,然后檢查newArray的長度。 如果長度為0,則沒有找到seach item,否則找到item。

您的工作解決方案

 const items = [ { name: "tomato", type: "produce", price: 0.99 }, { name: "soap", type: "cleaning", price: 0.99 }, { name: "t-shirt", type: "clothing", price: 5.99 } ] function searchItem(items, string) { let newArray = []; for (i = 0; i < items.length; i++) { if (string === items[i].name) { newArray.push(items[i]) break } } return newArray.length === 0? "Item not found": newArray; } const output = searchItem(items, "tomato"); const output1 = searchItem(items, "soap"); const output2 = searchItem(items, "t-shirt"); const output3 = searchItem(items, "t-shirts"); console.log(output); console.log(output1); console.log(output2); console.log(output3);

這可以使用Array.filter (查找字符串的多次出現)或Array.find (查找字符串的單個出現)輕松實現,而無需使用如下的 for 循環。

 const items = [ { name: "tomato", type: "produce", price: 0.99 }, { name: "soap", type: "cleaning", price: 0.99 }, { name: "t-shirt", type: "clothing", price: 5.99 } ] function searchItem(items, string) { let newArray = items.filter((item) => item.name === string); return newArray.length === 0? "Item not found": newArray; } const output = searchItem(items, "tomato"); const output1 = searchItem(items, "soap"); const output2 = searchItem(items, "t-shirt"); const output3 = searchItem(items, "t-shirts"); console.log(output); console.log(output1); console.log(output2); console.log(output3);

問題出在邏輯上,您試圖在循環中評估您的搜索結果。 這就是為什么正如@cjmling 所建議的那樣,如果您的搜索值與第一個索引不匹配,它將始終導致item not found 所以根據你的描述的解決方案是這樣的:

 let items = [ { name: "tomato", type: "produce", price: 0.99 }, { name: "soap", type: "cleaning", price: 0.99 }, { name: "t-shirt", type: "clothing", price: 5.99 } ] function searchItem(items,string) { let newArray = []; // if the cart is empty return this if (items.length === 0) { return "No items found in cart" } // if the cart is not empty run the loop for (i=0;i<items.length;i++) { if (string === items[i].name) { newArray.push(items[i]) } } // if search result is 0, then return this if (newArray.length === 0) { return ["Item not found"] } else { return newArray } } console.log(searchItem(items,"tomato")) console.log(searchItem(items,"soap")) console.log(searchItem([],"tomato")) console.log(searchItem(items,"human"))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM