从每个对象数组中获取最大值
Get max values from each array of objects
所以我有这样的数据:
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}]
]
我想获取每个对象数组中的最大值。所以结果应该是这样的:
maxValues = [150, 83, 55]
现在我的代码是:
let maxValues = []
let tmp;
let highest = Number.NEGATIVE_INFINITY;
data.map((eachArr, index) => {
for(let i = eachArr.length -1; i >= 0; i--){
tmp = eachArr[i].value;
if(tmp > highest) highest = tmp;
}
maxValues.push(highest)
})
结果是
maxValues = [150, 150, 150]
我怎样才能做到这一点?
您必须在 forEach
函数中包含 let highest = Number.NEGATIVE_INFINITY;
,因为您必须从 array
.
中的每个集合中设置 highest
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}]
]
let maxValues = []
let tmp;
data.forEach((eachArr, index) => {
let highest = Number.NEGATIVE_INFINITY;
for(let i = eachArr.length -1; i >= 0; i--){
tmp = eachArr[i].value;
if(tmp > highest) highest = tmp;
}
maxValues.push(highest)
})
console.log(maxValues);
您可以使用 spread syntax 并取映射的最大值 value
。
The spread syntax allows an expression to be expanded in places where multiple arguments (for function calls) or multiple elements (for array literals) or multiple variables (for destructuring assignment) are expected.
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 }]],
result = data.map(a => Math.max(...a.map(b => b.value)));
console.log(result);
具有 Math.max.apply()
和 Array.prototype.map()
功能的 Ecmascript 5 解决方案:
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 maxValues = data.map(function (arr) {
return Math.max.apply(null, arr.map(function(o){ return o.value; }));
});
console.log(maxValues);
Math.max.apply()函数将return给定数字中的最大值。
数字(value
属性每个嵌套对象)通过 Array.prototype.map() 函数累积。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max
Whitout ES6
_(data).map( function (item){
var max = _(item).max( function( hash ){ return hash.value })
return max.value
});
使用 ES6
_(data).map((item) => _(item).max(( hash ) => hash.value).value);
使用 Array#map
, to generate the array element find out an object which contains the largest value using Array#reduce
遍历数组,然后检索其值 属性。
data.map(arr => arr.reduce((a, b) => a.value < b.value ? b : a).value)
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.map((arr) => arr.reduce((a, b) => a.value < b.value ? b : a).value)
)
没有 ES6 arrow function :
data.map(function(arr){
return arr.reduce(function(a, b){
return a.value < b.value ? b : a
}).value;
});
所以我有这样的数据:
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}]
]
我想获取每个对象数组中的最大值。所以结果应该是这样的:
maxValues = [150, 83, 55]
现在我的代码是:
let maxValues = []
let tmp;
let highest = Number.NEGATIVE_INFINITY;
data.map((eachArr, index) => {
for(let i = eachArr.length -1; i >= 0; i--){
tmp = eachArr[i].value;
if(tmp > highest) highest = tmp;
}
maxValues.push(highest)
})
结果是
maxValues = [150, 150, 150]
我怎样才能做到这一点?
您必须在 forEach
函数中包含 let highest = Number.NEGATIVE_INFINITY;
,因为您必须从 array
.
highest
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}]
]
let maxValues = []
let tmp;
data.forEach((eachArr, index) => {
let highest = Number.NEGATIVE_INFINITY;
for(let i = eachArr.length -1; i >= 0; i--){
tmp = eachArr[i].value;
if(tmp > highest) highest = tmp;
}
maxValues.push(highest)
})
console.log(maxValues);
您可以使用 spread syntax 并取映射的最大值 value
。
The spread syntax allows an expression to be expanded in places where multiple arguments (for function calls) or multiple elements (for array literals) or multiple variables (for destructuring assignment) are expected.
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 }]],
result = data.map(a => Math.max(...a.map(b => b.value)));
console.log(result);
具有 Math.max.apply()
和 Array.prototype.map()
功能的 Ecmascript 5 解决方案:
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 maxValues = data.map(function (arr) {
return Math.max.apply(null, arr.map(function(o){ return o.value; }));
});
console.log(maxValues);
Math.max.apply()函数将return给定数字中的最大值。
数字(value
属性每个嵌套对象)通过 Array.prototype.map() 函数累积。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max
Whitout ES6
_(data).map( function (item){
var max = _(item).max( function( hash ){ return hash.value })
return max.value
});
使用 ES6
_(data).map((item) => _(item).max(( hash ) => hash.value).value);
使用 Array#map
, to generate the array element find out an object which contains the largest value using Array#reduce
遍历数组,然后检索其值 属性。
data.map(arr => arr.reduce((a, b) => a.value < b.value ? b : a).value)
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.map((arr) => arr.reduce((a, b) => a.value < b.value ? b : a).value)
)
没有 ES6 arrow function :
data.map(function(arr){
return arr.reduce(function(a, b){
return a.value < b.value ? b : a
}).value;
});