2017-05-14 51 views
-1

如何使用單個forin循環從json數據中搜索任何項目。 我曾嘗試搜索一個,但如果我想從其他領域搜索,該怎麼辦。 例如,如果我想搜索是否存在來自featuredBrands的項目並且存在是否返回該項目。如何搜索下面的json數據中的任何項目

var users = [{ 
 
    "userProfile": { 
 
     "firstName": "Rahul", 
 
     "lastName": "Jhawar", 
 
     "cartitemsno": 2, 
 
     "items": ["THINK AND GROW RICH", "Lenovo Z2 Plus"] 
 
    } 
 
    }, 
 

 
    { 
 
    "dayDeals": [{ 
 
     "productCategory": "Sports & Fitness Gear", 
 
     "discount": "20-80% off", 
 
     "items": "Yonex,Li-Ning & more" 
 
     }, 
 
     { 
 
     "productCategory": "Best Ethnic Trends", 
 
     "discount": "50-80% off", 
 
     "items": "Kurtas,Sarees & more" 
 
     }, 
 
     { 
 
     "productCategory": "Popular Brands", 
 
     "discount": "60-80% off", 
 
     "items": "T-shirts,Shirts,Jeans" 
 
     } 
 
    ] 
 
    }, 
 
    { 
 
    "featuredBrands": [{ 
 
     "brandName": "Ambrane", 
 
     "userRating": 3.7 
 
     }, 
 
     { 
 

 
     "brandName": "Sony", 
 
     "userRating": 4.2 
 

 
     }, 
 
     { 
 
     "brandName": "IPro", 
 
     "userRating": 4.0 
 
     } 
 
    ] 
 
    }, 
 
    { 
 

 
    "recommendedItems": [{ 
 
     "itemName": "Stay Hungry Stay Foolish", 
 
     "productCategory": "Books", 
 
     "prouctPrice": "Rs 186", 
 
     "offer": "3%" 
 
     }, 
 
     { 
 
     "itemName": "iPro IP 43 20800 mAh Power Bank (White & Grey, Lithium-ion)", 
 
     "productCategory": "Mobile Accessories", 
 
     "prouctPrice": "Rs 1199", 
 
     "offer": "60%" 
 
     }, 
 
     { 
 
     "itemName": "Micromax Canvas Pulse 4G (Grey, 16 GB) (3 GB RAM)", 
 
     "productCategory": "Mobile Phones", 
 
     "prouctPrice": "Rs 11199", 
 
     "offer": "12%" 
 
     } 
 
    ] 
 

 
    } 
 
] 
 

 
var displayitem = function(users, name) { 
 

 
    for (var item in users) { 
 

 
    if (users[item].userProfile.firstName == name) { 
 
     return users[item].userProfile.firstName; 
 
    } else { 
 
     return "Item not found" 
 
    } 
 
    } 
 
}; 
 

 
console.log(displayitem(users, "Rahul"));

+0

爲什麼你想只使用一個for循環?將多個循環嵌套在一起並不被認爲是「錯誤的」。此外,您的代碼將根據您要搜索的內容而有所不同... –

回答

0

您需要循環數組

見我的第二個例子了。你的JSON不合邏輯。第0項是userPRofile,則第1項是dayDeals和第2項特色品牌。數組中的第三個項目是recommendedItems,但他們都沒有涉及到用戶

var displayitem = function(users, name) { 
 

 
    for (var i = 0; i < users.length; i++) { 
 
    var userProfile = users[i].userProfile; 
 
    if (userProfile.firstName == name) { 
 
     return userProfile.items; 
 
    } 
 
    } 
 
    return "Item not found" 
 
}; 
 

 
var displayitem1 = function(users, name) { 
 
    for (var i = 0; i < users.length; i++) { 
 
    var recommendedItems = users[i].recommendedItems; 
 
    if (recommendedItems) { 
 
     for (var j = 0; j < recommendedItems.length; j++) { 
 
     if (recommendedItems[j].itemName == name) { 
 
      return i+"."+j+" name:"+name+" found"; 
 
     } 
 
     } 
 
    } 
 
    } 
 
    return false; 
 
}; 
 

 

 
var users = [{ 
 
    "userProfile": { 
 
     "firstName": "Rahul", 
 
     "lastName": "Jhawar", 
 
     "cartitemsno": 2, 
 
     "items": ["THINK AND GROW RICH", "Lenovo Z2 Plus"] 
 
    } 
 
    }, 
 

 
    { 
 
    "dayDeals": [{ 
 
     "productCategory": "Sports & Fitness Gear", 
 
     "discount": "20-80% off", 
 
     "items": "Yonex,Li-Ning & more" 
 
     }, 
 
     { 
 
     "productCategory": "Best Ethnic Trends", 
 
     "discount": "50-80% off", 
 
     "items": "Kurtas,Sarees & more" 
 
     }, 
 
     { 
 
     "productCategory": "Popular Brands", 
 
     "discount": "60-80% off", 
 
     "items": "T-shirts,Shirts,Jeans" 
 
     } 
 
    ] 
 
    }, 
 
    { 
 
    "featuredBrands": [{ 
 
     "brandName": "Ambrane", 
 
     "userRating": 3.7 
 
     }, 
 
     { 
 

 
     "brandName": "Sony", 
 
     "userRating": 4.2 
 

 
     }, 
 
     { 
 
     "brandName": "IPro", 
 
     "userRating": 4.0 
 
     } 
 
    ] 
 
    }, 
 
    { 
 

 
    "recommendedItems": [{ 
 
     "itemName": "Stay Hungry Stay Foolish", 
 
     "productCategory": "Books", 
 
     "prouctPrice": "Rs 186", 
 
     "offer": "3%" 
 
     }, 
 
     { 
 
     "itemName": "iPro IP 43 20800 mAh Power Bank (White & Grey, Lithium-ion)", 
 
     "productCategory": "Mobile Accessories", 
 
     "prouctPrice": "Rs 1199", 
 
     "offer": "60%" 
 
     }, 
 
     { 
 
     "itemName": "Micromax Canvas Pulse 4G (Grey, 16 GB) (3 GB RAM)", 
 
     "productCategory": "Mobile Phones", 
 
     "prouctPrice": "Rs 11199", 
 
     "offer": "12%" 
 
     } 
 
    ] 
 

 
    } 
 
] 
 

 
//console.log(displayitem(users, "Rahul")); 
 
console.log(displayitem1(users, "Stay Hungry Stay Foolish"));

+0

var displayitem = function(users,name){ for(var i = 0; i

+0

爲什麼上面的代碼不起作用 –

+0

查看更新。你的JSON不是你認爲的... – mplungjan

相關問題