2

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

2
  • Two pass deserializing, maybe? Try to deserialize to an array of arrays and if that fails (because it's not that format) try the "normal" deserialization. Commented Aug 7, 2014 at 18:50
  • Note that this is an array of arrays of arrays Commented Aug 7, 2014 at 18:54

5 Answers 5

6

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
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

You point in the right direction. Array.isArray() is right. But you have to check every element in the array.
@ThomasJunk Why would you? It's probably safe to assume that if the first item in the array is an array, then it's not point coordinates.
@plalx »It's probably safe to assume« d'accord. It depends on the source of the JSON and your knowledge about it. In some or maybe all cases you are right ;)
@ThomasJunk, as we are dealing with geometry, I assumed input would be consistent (either some path generators or user actions producing these coordinates). So IMO, a simple check would be sufficient.
2

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.

Comments

0

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
}

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.