私は以下のJSONデータを持っています。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
}
});
どのように私はこれを達成することができますか?
'array.indexOf(value.catName)== -1' //は、配列の – JohanP
可能な重複している[かどうかを確認する方法文字列配列にJavaScriptの文字列が1つ含まれていますか?](https://stackoverflow.com/questions/12623272/how-to-check-if-a-string-array-contains-one-string-in-javascript) – Nisarg
参照: https://stackoverflow.com/q/237104/5894241 – Nisarg