I wrote a restService and my service returns me two different result :
{"Response": {
"cars":[
{"id":"1",
"name":"test1"},
{"id":"2",
"name":"test2"},
{"id":"3",
"name":"test3"}
],
"books":[
{"id":"4",
"name":"example1"},
{"id":"5",
"name":"example2"},
{"id":"6",
"name":"example3"}
]}}
and second ,
{"Response": {
"cars":{
"id":"1",
"name":"test1"},
"books":{
"id":"4",
"name":"example1"}
}}
I will show the result in my dataTable with javascript. For this, I want to learn length of cars or books. If result >1 I can get length, cars or books act as an array,but if result <1 it acts as an object and I can't get length of objects .In my restService, I defined as an ArrayList the cars and books. Is there any solutions for this ? Thanks for responses.
var len = Array.isArray(Response.cars) ? Response.cars.length : 1Response.cars[i]in a for loop, it can't show to me because of it is a objectArray.isArray(Response.cars)<- that's how you know it's an array, if that fails, it's not an array, so you don't do aforloop at all.