ProgramKitkat
ProgramKitkat

Reputation: 395

Detect Array of Arrays

i have a json that may return something like this

"coordinates":
  [
    [[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]],
    [[100.2,0.2],[100.8,0.2],[100.8,0.8],[100.2,0.8],[100.2,0.2]]
  ]

Which will appear as an array of array Which I need to handle differently than something that may look like

"coordinates":[30.0,10.0]

But I was doing different actions based on the length of the coordinates array which in both cases is 2. (2 is a point else polygon or polyline) But I need to make sure that it isn't an array of arrays

Upvotes: 2

Views: 5464

Answers (5)

plalx
plalx

Reputation: 43718

Why can't you simply check whether the item you are processing is an array?

Array.isArray(coordinates[0]); would tell if the first item in the coordinates array is an array (considering that coordinates references the value of the coordinates property).

You could also do the opposite and check wheter the first item is a number.

function arePointCoords(coords) {
    return Array.isArray(coords) && typeof coords[0] === 'number';
}


arePointCoords([30.0,10.0]); //true
arePointCoords([
    [[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]],
    [[100.2,0.2],[100.8,0.2],[100.8,0.8],[100.2,0.8],[100.2,0.2]]
]); //false

Please note that using names closely related to the domain rather than technical terms e.g. arePointCoords(coords) rather than !isArrayOfArrays(coords) also makes your code easier to understand. However, arePointCoords could rely on a more generic function such as isArrayOfArrays internally.

Upvotes: 0

Thomas Junk
Thomas Junk

Reputation: 5676

But I need to make sure that it isn't an array of arrays

What about a simple check-Function:

var a1=[1,2,3,4];
var a2=[[1],[2]];

function checkArrayOfArrays(a){
    return a.every(function(x){ return Array.isArray(x); });
}

console.log(checkArrayOfArrays(a1));
console.log(checkArrayOfArrays(a2));

JSBIN to play with. MDN documentation to Array.prototype.every().

Edit: Of course there is the case, that you have a mix-state, which in this case would be recognized as false, which isn't always desireable. Then Array.prototype.some() comes to the rescue.

Upvotes: 2

Rajkumar Madhuram
Rajkumar Madhuram

Reputation: 684

Maybe something like this?

if (Array.isArray(coordinates)) {
  // is an array
  if (Array.isArray(coordinates[0])) {
    // is an array of array
    // process polyline
  } else {
    // process point
  }
}

Upvotes: 6

Jeremy J Starcher
Jeremy J Starcher

Reputation: 23863

A function like this:

var array = [30, 31, 32];
var nestedarray = [
  [1, 2, 3]
];

function isArray(obj) {
  // Under ES5 you could use obj.isArray
  return toString.call(obj) === "[object Array]";
}

function isNestedArray(a) {
  // Is the thing itself an array?
  // If not, can't be a nested array
  if (!isArray(a)) {
    return false;
  }

  // Is the first element an array?
  if (isArray(a[0])) {
    return true;
  }

  // Otherwise, nope, not a nested array
  return false;
}

console.log(isNestedArray(array));
console.log(isNestedArray(nestedarray));
console.log(isNestedArray(null));

Upvotes: 0

sifriday
sifriday

Reputation: 4462

Given:

bar = {"coordinates":[30.0,10.0]}

typeof bar.coordinates[0]
"number"

foo = {"coordinates":
  [
    [[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]],
    [[100.2,0.2],[100.8,0.2],[100.8,0.8],[100.2,0.8],[100.2,0.2]]
  ]}

typeof foo.coordinates[0]
"object"

Then you can do:

foo = (deserialise your JSON here)
if (typeof foo.coordinates[0] == "object") {
   // handle polyline
}
else {
   // handle poinmt
}

Upvotes: 0

Related Questions