admin管理员组文章数量:1320780
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
5 Answers
Reset to default 6Maybe 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.
本文标签: javascriptDetect Array of ArraysStack Overflow
版权声明:本文标题:javascript - Detect Array of Arrays - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742090064a2420210.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论