Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if the JSON Object array contains the value defined in an arrary or not?

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 looping the JSON data I need to check if the Object value is listed in an array or not. If the yes do something else do another thing.

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?

like image 808
Kiran Shahi Avatar asked Nov 12 '17 08:11

Kiran Shahi


People also ask

How do I check if a JSON value is an array?

Check if Variable is Array ? Sometimes, you need to check a parsed JSON object or a variable to see if it's an array before iteration or before any other manipulation. You can use the Array. isArray method to check if a variable is an array.

How do you check if a JSON object contains a key or not?

Use below code to find key is exist or not in JsonObject . has("key") method is used to find keys in JsonObject . If you are using optString("key") method to get String value then don't worry about keys are existing or not in the JsonObject . Note that you can check only root keys with has(). Get values with get().

How do you check if an array contains an element?

The simplest and fastest way to check if an item is present in an array is by using the Array. indexOf() method. This method searches the array for the given item and returns its index. If no item is found, it returns -1.


2 Answers

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>
like image 175
Mamun Avatar answered Oct 25 '22 00:10

Mamun


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.

like image 29
dhilt Avatar answered Oct 24 '22 22:10

dhilt