所以,我是 JavaScript 的新手,我在讓函式按照我想要的方式作業時遇到了麻煩。就目前而言,它可以很好地列印“番茄”的內容,并且可以判斷一個專案是否不在串列中......有點。如果我輸入“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")
uj5u.com熱心網友回復:
這是searchItem函式中處理邏輯的問題。
"Item not found"只有在回圈執行完成后才應該設定,否則如果陣列中的第一項與搜索元素不匹配,它將"Item not found"不檢查其下方的項而回傳。
在您的代碼中,您正在遍歷items陣列并檢查陣列name中每個專案的items。如果名稱與函式的引數匹配,則將其推送到輸出陣列并破壞函式。如果您只想找到具有該值的第一個元素,在那里添加一個中斷就可以了。如果您想找到該字串的所有組合,您可以避免在該行中斷(我采用了這種方法)。
問題出在else if部分。如果name此陣列中的項與字串不匹配,它將推"Item not found"送到newArray并中斷回圈。所以這將中斷陣列中第一個不匹配的元素并停止執行進一步的回圈。所以如果搜索元素不在第一個索引處,函式將停止執行并推"Item not found"送到"newArray"和 中斷。其余的代碼塊將永遠不會執行。
要使您的函式正常作業,請將else-if,else移出回圈。讓回圈完成執行,然后檢查newArray. 如果長度為0,則未找到搜索項,否則找到項。
編輯:你也必須整合"No items found in cart"邏輯。為此,您必須檢查購物車輸入的長度。如果它為零,你必須回傳"No items found in cart"
您的作業解決方案
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(cart, string) {
if (cart.length === 0) {
return "No items found in cart";
}
let newArray = [];
for (i = 0; i < cart.length; i ) {
if (string === cart[i].name) {
newArray.push(cart[i])
break
}
}
return newArray.length === 0 ? "Item not found" : newArray;
}
const emptyCart = [];
const output = searchItem(items, "tomato");
const output1 = searchItem(items, "soap");
const output2 = searchItem(items, "t-shirt");
const output3 = searchItem(items, "t-shirts");
const emptySearch = searchItem(emptyCart, "tomato");
console.log(output);
console.log(output1);
console.log(output2);
console.log(output3);
console.log(emptySearch);
這可以很容易地使用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(cart, string) {
if (cart.length === 0) {
return "No items found in cart";
}
let newArray = cart.filter((item) => item.name === string);
return newArray.length === 0 ? "Item not found" : newArray;
}
const emptyCart = [];
const output = searchItem(items, "tomato");
const output1 = searchItem(items, "soap");
const output2 = searchItem(items, "t-shirt");
const output3 = searchItem(items, "t-shirts");
const emptySearch = searchItem(emptyCart, "tomato");
console.log(output);
console.log(output1);
console.log(output2);
console.log(output3);
console.log(emptySearch);
uj5u.com熱心網友回復:
問題出在邏輯上,您試圖在回圈中評估搜索結果。這就是為什么正如@cjmling 所建議的那樣,如果您的搜索值與第一個索引不匹配,它將始終導致item not found. 所以根據你的描述,解決方案是這樣的:
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 = [];
// 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"));
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/359489.html
標籤:javascript
