- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有这样的数据:
data = [
[{a: "b", value: 12}, {a: "bb", value: 39}, {a: "bb", value: 150}],
[{a: "c", value: 15}, {a: "cc", value: 83}, {a: "ccc", value: 12}],
[{a: "d", value: 55}, {a: "dd", value: 9}, {a: "dd", value: 1}]
]
我想在这个数据中获取相同索引处的最大值,所以我希望结果是这样的:
[55, 83, 150]
现在,我可以获取对象中的每个值,如果我指定索引,我可以获取最大值。我不确定如何处理每个索引。
let array = [];
data.map((eachArr, index) => {
array.push(eachArr[0].value)
for(let i = 0; i < eachArr.length; i++){
console.log('eachArr[i].value', eachArr[i].value, i);
}
})
console.log(Math.max(...array)) ===> 55
我该怎么做?
我认为人们误解了我的问题。我不想要每个数组中的最大值。我想要每个数组中具有相同索引的 value
的最大值。所以我想要 55 来自 12、15、55、83 来自 39、83、9 和 150 来自 150、12、1。很抱歉没有具体说明。我应该有一个不同长度的例子。
最佳答案
使用Array#reduce
使用 Array#forEach
的方法方法。
var data = [[{ a: "b",value: 12}, { a: "bb",value: 39 }, { a: "bb", value: 150 }], [{ a: "c", value: 15 }, { a: "cc", value: 83 }, { a: "ccc", value: 12 }], [{ a: "d", value: 55 }, { a: "dd", value: 9 }, { a: "dd", value: 1 }]];
console.log(
// iterate over the array
data.reduce(function(arr, ele) {
// iterate over the inner array
ele.forEach(function(o, i) {
// check element present at the index, if not then update with current value
arr[i] = arr[i] || o.value;
// check assign greatest value by comparing with previous value
arr[i] = arr[i] < o.value ? o.value : arr[i];
// you can combine above two lines
// arr[i] = !arr[i] || arr[i] < o.value ? o.value : arr[i];
});
// return the array reference
return arr;
// set initial value as an empty array
}, [])
)
// without any comments
console.log(
data.reduce(function(arr, ele) {
ele.forEach(function(o, i) {
arr[i] = !arr[i] || arr[i] < o.value ? o.value : arr[i];
});
return arr;
}, [])
)
let data = [[{ a: "b",value: 12}, { a: "bb",value: 39 }, { a: "bb", value: 150 }], [{ a: "c", value: 15 }, { a: "cc", value: 83 }, { a: "ccc", value: 12 }], [{ a: "d", value: 55 }, { a: "dd", value: 9 }, { a: "dd", value: 1 }]];
console.log(
data.reduce((arr, ele) => (ele.forEach((o, i) => arr[i] = !arr[i] || arr[i] < o.value ? o.value : arr[i]), arr), [])
)
var data = [[{ a: "b",value: 12}, { a: "bb",value: 39 }, { a: "bb", value: 150 }], [{ a: "c", value: 15 }, { a: "cc", value: 83 }, { a: "ccc", value: 12 }], [{ a: "d", value: 55 }, { a: "dd", value: 9 }, { a: "dd", value: 1 }]];
var res = [];
for (var i = 0; i < data.length; i++) {
for (var j = 0; j < data[i].length; j++) {
res[j] = !res[j] || res[j] < data[i][j].value ? data[i][j].value : res[j];
}
}
console.log(res);
仅供引用: Performance comparison
关于javascript - 如何在数组数组中的相同索引处获取最大值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41843872/
我有一个像 [3,10,4,3,9,15,6,13] 这样的列表,我想找到两个不重叠的系列/序列给出通过取最大-最小值可获得的最大值.它们必须是连续的,因此您不能从 1 中减去项目 3。但是您可以从
我正在尝试创建顶部列,这是几个列行的最大值。 Pandas 有一个方法 nlargest但我无法让它成行工作。 Pandas 也有 max和 idxmax这正是我想做的,但仅限于绝对最大值。 df =
我在使用 Android 时遇到了一点问题。 我有我的 GPS 位置,明确的经纬度,以及以米为单位的搜索射线(例如 100 米),可以吗? 想象一下我在射线形成的圆心的位置,我会知道如何在 Andro
假设我有一组最小值和最大值。我想要一个数据结构,在给定外部值的情况下,它会最有效地为我提供值 >= 最小值、值 = 最小值和值 <= 最大值?,我们在Stack Overflow上找到一个类似的问题:
我有以下 Maxima 代码: m:sum(x[i],i,1,N)/N; 然后我想计算 $m^2$。 m2:m^2, sumexpand; 然后我得到双重求和: sum(sum(x[i1]*x[i2]
如何从嵌套字典中获取一个值的最小值/最大值,该字典的缺失值也包含“Nan”? *这是供引用,我找到了一个解决方案,我想我应该在这里分享它,因为我在 stackoverflow 上的任何地方都找不到答案
在千里马 12.04.0 我有一个总和 mysum : sum(u[i]^2, i, 1, N); 现在我区分它 diff(mysum, u[i]); 现在我指定一个定义的索引 i=A 来区分它 at
是否可以根据时间轴获取最小和最大时间戳?我将在 parking 场示例中进行解释。 +---------------------+------+--------+-------+-----------
基本上在几个领域有几个日期 SELECT MAX(MAX(DATE_A),MAX(DATE_B)) from table DATE_A 和 DATE_B 是日期,我基本上想要日期 A 或日期 B 的最
我创建了一个小测试,其中一个 div 根据滚动深度滑动。 我只是想知道怎么设置 A) 起点 (scrolltop = x something) B) 如何设置最大值? var pxlCount = 0
由于达到最大值,clock_gettime() 何时会使用 CLOCK_MONOTONIC 返回一个较小的值?我不是指被描述为错误的小扭曲,而是类似于计数器重置的东西。 它是时间测量的,还是与滴答的绝
我正在使用 angularjs,尤其是 $timeout 服务(setTimeout 的包装器)。它的工作原理如下: angular.module('MyApp').controller('MyCo
是否有可能获得 MinValue - 或 MaxValue未知的 T 型?如 Int其中有 Int.MinValue和 Int.MaxValue ?? 谢谢 最佳答案 正如@mpilquist 在上面
我的数据为 员工: id Name -------- 1 xyz 2 abc 3 qaz Employee_A:(Eid - 员工表,title - 职称表) eid active
我有一个日期和时间行列表,每天有多行。 对于每个唯一日期,我想获取最小和最大时间值。 如何在 Excel v10(又名 2002)中执行此操作? 最佳答案 首先,您可以使用 Excel 函数 MIN(
我有以下 SQL 表 - Date StoreNo Sales 23/4 34 4323.00 23/4 23 5
我可能错过了一些微不足道的东西。我想我还没有完全理解一些基本的交叉过滤器概念 无论如何,我创建了一个带有几个维度的交叉过滤器,并在维度上使用过滤器。我现在想知道过滤值(不是键)的最小值/最大值。 我将
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 9 年前。 Improve t
我在这里错过了什么吗?我希望以下代码段中的 np.max 会返回 [0, 4] ... >>> a array([[1, 2], [0, 4]]) >>> np.max(a,
给定大小为 2 的列表列表,我试图找到通过索引确定最小/最大值的最快方法。目标是确定一系列 XY 点的边界/范围。 子列表未排序(按一个索引排序并不能保证另一个索引已排序)。 目前我正在做以下事情:
我是一名优秀的程序员,十分优秀!