我有以下JSON数据。
categories = [
{catValue:1, catName: 'Arts, crafts, and collectibles'},
{catValue:2, catName: 'Baby'},
{catValue:3, catName: 'Beauty and fragrances'},
{catValue:4, catName: 'Books and magazines'},
{catValue:5, catName: 'Business to business'},
{catValue:6, catName: 'Clothing, accessories, and shoes'},
{catValue:7, catName: 'Antiques'},
{catValue:8, catName: 'Art and craft supplies'},
{catValue:9, catName: 'Art dealers and galleries'},
{catValue:10, catName: 'Camera and photographic supplies'},
{catValue:11, catName: 'Digital art'},
{catValue:12, catName: 'Memorabilia'}
];
var categoriesJson = JSON.stringify(categories);
和以下数组。
var mainCat = ['Arts, crafts, and collectibles', 'Baby' , 'Antiques']
在循环JSON数据时,我需要检查Object值是否在数组中列出,如果是,则执行其他操作。
例如
$.each(categoriesJson , function (key, value) {
if(value.catName is in array) {
//do something here
} else {
//do something here
}
});
我怎样才能做到这一点?
3条答案
按热度按时间wz3gfoph1#
请尝试以下操作:
z4iuyo4d2#
我将filter初始数据数组以获得匹配类别的数组:
然后,您可以通过迭代这个子数组来完成您需要的操作。
mwecs4sa3#
我也使用 @dhilt 方法,但使用包含
例如,如果包含一个(返回bool)