-1

I am not so into JavaScript and I have the following problem. To solve this problem I can use only pure old JavaScript (not library or framework).

I have a JSON document like this:

{
    "Commodity": {
        "CommodityInMarket": {
            "market_info": {
                "market_id": 5,
                "market_name": "Tambacounda Market N2",
                "available_market_count": 1
            },
            "commodity_info": {
                "commodity_details_id": 1,
                "commodity_name_en": "Rice-Asia",
                "commodity_name": "Rice-Asia",
                "image_link": "https://firebasestorage.googleapis.com/v0/b/fao-digital-services-portfolio.appspot.com/o/img%2Ficons%2Fagrimarket%2Fcommodity%2Friz.png?alt=media&token=c35e7648-1793-423b-acd2-52d8a1e58c53",
                "description": "Rice-Asia"
            },
            "price_info": {
                "price_serie_id": 29,
                "today_avg_price": 9.3000,
                "yesterday_avg_price": 9.6000,
                "currency": "XOF",
                "measure_unit": "kilogram"
            }
        }
    }
}

As you can see this document contains a Commodity object.

In some case it is a simple object (as in the previous code snippet), in some other case it can be an array (this document is returned by a service in this way), so something like this:

{
    "Commodity": {
        "CommodityInMarket": [{
            .....................................
            .....................................
            .....................................
        },
        {
            .....................................
            .....................................
            .....................................
        }]
    }
}

How can I check if this Commodity object into my JSON document is a simple object or an array? I need to do something like this:

If it is a simple object put true into a boolean variable. How can I implement this behavior?

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596

3 Answers3

2

Taken from this answer:

The method given in the ECMAScript standard to find the class of Object is to use the toString method from Object.prototype.

if (Object.prototype.toString.call(obj.Commodity.CommodityInMarket) === '[object Array]') {
  //Loop through obj.Commodity.CommodityInMarket
} else {
  //It's an object, just access directly
}

Another way could be to just automatically convert it to an array, and always loop through it:

if (!Object.prototype.toString.call(obj.Commodity.CommodityInMarket) === '[object Array]') {
  obj.Commodity.CommodityInMarket = [obj.Commodity.CommodityInMarket];
}
//obj.Commodity.CommodityInMarket should now always be an array
Blue
  • 22,608
  • 7
  • 62
  • 92
0

As of ECMAScript 5.1 there is also Array.isArray().

Dormilich
  • 927
  • 5
  • 11
0

I would create consistency by converting the single object into an array containing that object:

obj.Commodity.CommodityInMarket =
  [].concat(obj.Commodity.CommodityInMarket);
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151