@scx-js/scx-common 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/util/ArrayUtils.js +10 -5
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scx-js/scx-common",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "SCX Common",
5
5
  "license": "MIT",
6
6
  "author": "scx567888",
@@ -140,11 +140,7 @@ function arrayEquals(array1, array2) {
140
140
  * @param item
141
141
  */
142
142
  function removeAllByItem(list, item) {
143
- for (let i = list.length - 1; i >= 0; i--) {
144
- if (list[i] === item) {
145
- removeByIndex(list, i);
146
- }
147
- }
143
+ removeIf(list, (c) => c === item);
148
144
  }
149
145
 
150
146
  /**
@@ -164,6 +160,14 @@ function uniqueArray(list) {
164
160
  list.length = j; // 截断数组长度
165
161
  }
166
162
 
163
+ function removeIf(list, filter) {
164
+ for (let i = list.length - 1; i >= 0; i--) { // 从后向前遍历,避免索引错位
165
+ if (filter(list[i])) {
166
+ removeByIndex(list, i);
167
+ }
168
+ }
169
+ }
170
+
167
171
  export {
168
172
  copyArray,
169
173
  removeByIndex,
@@ -176,4 +180,5 @@ export {
176
180
  arrayEquals,
177
181
  removeAllByItem,
178
182
  uniqueArray,
183
+ removeIf,
179
184
  };