最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to check if the JSON Object array contains the value defined in an array or not? - Stack Overflow

programmeradmin2浏览0评论

I have the following JSON data.

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);

And following array.

var mainCat = ['Arts, crafts, and collectibles', 'Baby' , 'Antiques']

While iterating through the JSON data, I need to check if the object value is included in an array. If it exists, do one thing; otherwise, do another.

For example

$.each(categoriesJson , function (key, value) {
    if(value.catName is in array) {
        //do something here 
    } else {
        //do something here
    }
});

How can I achieve this?

I have the following JSON data.

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);

And following array.

var mainCat = ['Arts, crafts, and collectibles', 'Baby' , 'Antiques']

While iterating through the JSON data, I need to check if the object value is included in an array. If it exists, do one thing; otherwise, do another.

For example

$.each(categoriesJson , function (key, value) {
    if(value.catName is in array) {
        //do something here 
    } else {
        //do something here
    }
});

How can I achieve this?

Share Improve this question edited May 16, 2024 at 13:23 Kiran Shahi asked Nov 12, 2017 at 8:49 Kiran ShahiKiran Shahi 7,9809 gold badges40 silver badges76 bronze badges 3
  • 1 array.indexOf(value.catName) !== -1 // is in array – JohanP Commented Nov 12, 2017 at 8:51
  • Possible duplicate of How to check if a string array contains one string in JavaScript? – Nisarg Shah Commented Nov 12, 2017 at 8:51
  • Also see: stackoverflow.com/q/237104/5894241 – Nisarg Shah Commented Nov 12, 2017 at 8:52
Add a comment  | 

3 Answers 3

Reset to default 6

Try the following:

var 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']

$.each(JSON.parse(categoriesJson) , function (key, value) {
  if(mainCat.indexOf(value.catName) > -1){
   console.log('Exists: ' +value.catName)
 }
 else{
   console.log('Does not exists: ' +value.catName)
 }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

I would filter the initial data array to get the array of matching categories:

var matchedCategories = categories.filter(i => mainCat.indexOf(i.catName) >= 0);

Then you could do what you need by iterating this sub-array.

I also use @dhilt approach but with includes

ex. If even one is included (return bool)

  categories.filter(i =>
    mainCat.includes(i.catName)
  ).length > 0
    ? true
    : false;
发布评论

评论列表(0)

  1. 暂无评论