我有以下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数据时,我需要检查对象值是否列在数组中。如果是,则执行某些操作,否则执行其他操作。
例如:
$.each(categoriesJson , function (key, value) {
if(value.catName is in array) {
//do something here
} else {
//do something here
}
});
我该如何实现这个目标?
array.indexOf(value.catName) !== -1
// 在数组中 - JohanP