检测阵列阵列

Pro*_*kat 1 javascript arrays json

我有一个 json 可能会返回这样的东西

"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]]
  ]
Run Code Online (Sandbox Code Playgroud)

这将显示为一个数组数组,我需要以不同于看起来像的东西来处理

"coordinates":[30.0,10.0]
Run Code Online (Sandbox Code Playgroud)

但是我根据坐标数组的长度进行了不同的操作,这两种情况下都是 2。(2 是一个点,其他多边形或折线)但我需要确保它不是数组数组

Raj*_*ram 5

也许是这样的?

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