Skip to content

Commit 33775d9

Browse files
committed
feat: 修改了代码段
1 parent 5a6205e commit 33775d9

5 files changed

Lines changed: 47 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@
575575
- [排序索引值--2023-04-18](codes/javascript/sorted-index.md)
576576
- [根据回调函数查找排序索引值--2023-04-19](codes/javascript/sorted-index-by.md)
577577
- [查找排序最后插入索引值--2023-04-20](codes/javascript/sorted-last-index.md)
578-
- [根据回调函数查找排序最后插入索引值--2023-04-21](codes/javascript/sortedLastIndexBy.md)
578+
- [根据回调函数查找排序最后插入索引值--2023-04-21](codes/javascript/sorted-last-index-by.md)
579579
- [字符串分割成数组--2023-04-22](codes/javascript/splitLines.md)
580580
- [处理参数的函数--2023-04-23](codes/javascript/spreadOver.md)
581581
- [稳定排序--2023-04-24](codes/javascript/stableSort.md)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>sorted-last-index-by</title>
8+
</head>
9+
10+
<body>
11+
<script>
12+
const sortedLastIndexBy = (arr, n, fn) => {
13+
const isDescending = fn(arr[0]) > fn(arr[arr.length - 1]);
14+
const val = fn(n);
15+
const index = arr
16+
.map(fn)
17+
.reverse()
18+
.findIndex(el => (isDescending ? val <= el : val >= el));
19+
return index === -1 ? 0 : arr.length - index;
20+
};
21+
</script>
22+
</body>
23+
24+
</html>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,20 @@ const sortedLastIndexBy = (arr, n, fn) => {
2020
};
2121
```
2222

23+
<div class="code-editor" data-url="codes/javascript/ts/sorted-last-index-by.ts" data-language="typescript"></div>
24+
2325
> 调用方式:
2426
2527
```js
2628
sortedLastIndexBy([{ x: 4 }, { x: 5 }], { x: 4 }, o => o.x); // 1
2729
```
2830

2931
> 应用场景
32+
33+
以下是一个实战<a href="codes/javascript/html/sorted-last-index-by.html" target="_blank" rel="noopener noreferrer">示例</a>:
34+
35+
<div class="code-editor" data-url="codes/javascript/html/sorted-last-index-by.html" data-language="html"></div>
36+
37+
结果如下:
38+
39+
<iframe src="codes/javascript/html/sorted-last-index-by.html"></iframe>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const sortedLastIndexBy = <T>(arr: T[], n: T, fn: (v: T) => number) => {
2+
const isDescending = fn(arr[0]) > fn(arr[arr.length - 1]);
3+
const val = fn(n);
4+
const index = arr
5+
.map(fn)
6+
.reverse()
7+
.findIndex(el => (isDescending ? val <= el : val >= el));
8+
return index === -1 ? 0 : arr.length - index;
9+
};
10+
11+
sortedLastIndexBy([{ x: 4 }, { x: 5 }], { x: 4 }, o => o.x); // 1

pages/sidebar.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@
568568
- [排序索引值--2023-04-18](codes/javascript/sorted-index.md)
569569
- [根据回调函数查找排序索引值--2023-04-19](codes/javascript/sorted-index-by.md)
570570
- [查找排序最后插入索引值--2023-04-20](codes/javascript/sorted-last-index.md)
571-
- [根据回调函数查找排序最后插入索引值--2023-04-21](codes/javascript/sortedLastIndexBy.md)
571+
- [根据回调函数查找排序最后插入索引值--2023-04-21](codes/javascript/sorted-last-index-by.md)
572572
- [字符串分割成数组--2023-04-22](codes/javascript/splitLines.md)
573573
- [处理参数的函数--2023-04-23](codes/javascript/spreadOver.md)
574574
- [稳定排序--2023-04-24](codes/javascript/stableSort.md)

0 commit comments

Comments
 (0)