如何使用单个forin循环从json数据中搜索任何项目。 我试过搜索一个,但如果我想从任何其他领域搜索该怎么办。 例如,如果我想搜索来自featuredBrands的项目是否存在,并且是否存在则返回该项目。
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 :(得分:0)
你需要遍历数组
见我的第二个例子。你的JSON不合逻辑。第0项是userPRofile,然后第1项是dayDeals,第2项是特色品牌。数组中的第3项是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"));