如何从object-Javascript数组计算最小/最大值

时间:2017-05-30 23:35:51

标签: javascript arrays

我有一个数据显示图表上的各种数据点。

var data = [{name:"s", y:2},{name:"e",y:90},{name:"tt",y:9},{name:"se",y:10}]

现在我知道使用简单数组你可以使用Math.max ....和Math.min来计算最小/最大值,不知道如何用对象数组实现相同的效果?

谢谢!

2 个答案:

答案 0 :(得分:1)

您可以使用df = pd.concat( [df.join(pd.DataFrame( np.random.randint(10, size=(3, 22)), columns=list(ascii_uppercase[4:]) ))] * 10000, ignore_index=True ) %%timeit a = df[['A', 'C']].values The slowest run took 23.28 times longer than the fastest. This could mean that an intermediate result is being cached. 1000 loops, best of 3: 371 µs per loop In [305]: %%timeit c = ['A', 'C'] p = [df.columns.get_loc(i) for i in c] a = df.values[:, p].astype(df.dtypes[c[0]]) 100 loops, best of 3: 9.62 ms per loop %timeit np.column_stack([df[col].values for col in ['A', 'C']]) The slowest run took 6.66 times longer than the fastest. This could mean that an intermediate result is being cached. 10000 loops, best of 3: 55.6 µs per loop 获取正确的最小值和最大值。

.map

如果您使用的是var data = [{name:"s", y:2},{name:"e",y:90},{name:"tt",y:9},{name:"se",y:10}]; var values = data.map(function( obj ) { return obj.y; }); var min = Math.min.apply(null, values); var max = Math.max.apply(null, values); ,那么您可以摆脱ES6并将其替换为传播运营商。

.apply

答案 1 :(得分:0)

您仍然可以使用Math.maxMath.min。首先只需map数据。

const max = Math.max.apply(null, data.map((point) => point.y));
const min = Math.min.apply(null, data.map((point) => point.y));