2017-05-14 24 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

相关问题