Math.max.apply在非对象上调用CreateListFromArrayLike错误

时间:2017-10-20 09:00:43

标签: javascript arrays json

我正在研究什么是最大值,最小值是什么。为此,我使用Math.max.apply()Math.min.apply()。 在我检查之前,我将一个Object转换为从和调用的GeoJson代码。 看起来像这样:

{
	"type": "FeatureCollection",
	"features": [
		{
			"type": "Feature",
			"geometry": {
				"type": "Polygon",
				"coordinates": [
					[
						[
							6.338356,
							52.790868
						],
						[
							6.378572,
							52.648089
						],
						[
							6.10522,
							52.663572
						],
						[
							6.089325,
							52.798539
						],
						[
							6.338356,
							52.790868
						]
					]
				]
			},
			"properties": {
				"name": "DiggitTwoOne",
				"regioFacetId": "tcm:106-353682-1024",
				"level": 3,
				"from": "10",
				"to": "12"
			}
		},
		{
			"type": "Feature",
			"geometry": {
				"type": "Polygon",
				"coordinates": [
					[
						[
							6.105498,
							52.663816
						],
						[
							6.378605,
							52.648187
						],
						[
							6.417051,
							52.520692
						],
						[
							6.173867,
							52.509454
						],
						[
							6.105498,
							52.663816
						]
					]
				]
			},
			"properties": {
				"name": "DiggitTwoTwo",
				"regioFacetId": "tcm:106-353682-1024",
				"level": 3,
				"from": "13",
				"to": "15"
			}
		}
  ]
}

现在javascript代码如下所示:

var from = event.feature.getProperty("from");
var to = event.feature.getProperty("to");
var mergeObjects = JSON.parse(from,to);

console.log(Math.max.apply(Math,mergeObjects));
console.log(Math.min.apply(Math, mergeObjects));

当我运行javascript代码时,会出现如下错误: CreateListFromArrayLike在非对象上调用。

我正在使用对象。

我做错了什么,或者我是否错误地使用了它。

1 个答案:

答案 0 :(得分:0)

你的问题是apply接受了一个数组而你没有传递它:

var from = event.feature.getProperty("from"),
    to = event.feature.getProperty("to"),
    numArr = [from, to];

Math.max.apply(Math, numArr);
Math.min.apply(Math, numArr);

这是你要找的吗?