比如
data:[ [1,3,5] , [3,5,6] , [4,2,1] ]
返回【3,5,6】就是最后一项最大的那个子数组。
比如
data:[ [1,3,5] , [3,5,6] , [4,2,1] ]
返回【3,5,6】就是最后一项最大的那个子数组。
这样吗?
例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let data1 = [[1, 3, 5], [3, 5, 6], [4, 2, 1]]
let data2 = [[1, 3, 5], [3, 5, 4], [4, 2, 1]]
let data3 = [[1, 3, 5], [3, 5, 6], [4, 2, 7]]
/**
* 获取最后一项最大的数组
*/
function getLastMaxArr(arr) {
// 校验arr字段是否合规我就不写了🤭
let res = arr[0];
let lastMax = arr[0][arr[0].length - 1];
for (let i = 1; i < arr.length; i++) {
let cArr = arr[i];
let tmpLastMax = arr[i][arr[i].length - 1];
if (lastMax < tmpLastMax) {
lastMax = tmpLastMax;
res = cArr;
}
}
return res;
}
console.log(getLastMaxArr(data1));
console.log(getLastMaxArr(data2));
console.log(getLastMaxArr(data3));
</script>
</body>
</html>