💾arr.removeValue()
removes specific value from array.
/**
* remove specific `value` from the array.
* @example
* import array_removeValue from './.../array_removeValue.mjs';
* Object.assign(Array.prototype, array_removeValue);
* @mixin
*/
const array_removeValue = {
/**
* remove specific `value` from the array.
* @example
* const arr = [1, 2, 1, 3, 1, 4]
* arr.removeValue(1) // [ 2, 3, 4 ]
* @param value {*} the value to be removed
*/
removeValue(value) {
return this.filter(x => x !== value);
},
};
export default array_removeValue;
💈範例:
import array_removeValue from '../objects/mixin/array_removeValue.mjs';
Object.assign(Array.prototype, array_removeValue);
const arr = [1, 2, 1, 3, 1, 4]
arr.removeValue(1) // [ 2, 3, 4 ]
arr.filter() - remove undefined, nullish, holes, ... from array.
Archive
replit ⟩ array ⟩ removeAll()
/**
* common tools for Array
* @mixin
*/
const ArrayTools = {
/**
* remove every `value` from the array IN PLACE. ⭐️
* @example
* // extend Array by mixin
* Object.assign(Array.prototype, ArrayTools);
* const a = [1, 2, 1, 3, 1, 4];
* a.removeAll(1); // [ 2, 3, 4 ]
* @param value {*} the value to be removed
*/
removeAll(value) {
for (let i = 0; i < this.length; i++) {
if (this[i] === value) {
this.splice(i, 1); // remove one item (in place)
i -= 1; // keep i the same in the next loop
}
}
}
};
💈範例:
const a = [1, 2, 1, 3, 1, 4];
Object.assign(Array.prototype, ArrayTools);
a.removeAll(1);
a; // [2,3,4]
Last updated