最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Detect Array of Arrays - Stack Overflow

programmeradmin6浏览0评论

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

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

Share Improve this question edited Aug 7, 2014 at 18:53 ProgramKitkat asked Aug 7, 2014 at 18:47 ProgramKitkatProgramKitkat 3951 gold badge4 silver badges9 bronze badges 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. – JBC Commented Aug 7, 2014 at 18:50
  • Note that this is an array of arrays of arrays – Pablo Matias Gomez Commented Aug 7, 2014 at 18:54
Add a ment  | 

5 Answers 5

Reset to default 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
  }
}

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() es to the rescue.

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
}

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

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.

发布评论

评论列表(0)

  1. 暂无评论