36 lines
1.2 KiB
JavaScript
36 lines
1.2 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.default = deleteByKey;
|
|
const lodash_1 = __importDefault(require("lodash"));
|
|
/**
|
|
* # Delete all matches in an Array
|
|
*/
|
|
function deleteByKey(arr, key) {
|
|
let newArray = lodash_1.default.cloneDeep(arr);
|
|
for (let i = 0; i < newArray.length; i++) {
|
|
const item = newArray[i];
|
|
if (Array.isArray(key)) {
|
|
const targetMatches = [];
|
|
for (let k = 0; k < key.length; k++) {
|
|
const ky = key[k];
|
|
const targetValue = item[ky];
|
|
const targetOriginValue = item[ky];
|
|
targetMatches.push(targetValue == targetOriginValue);
|
|
}
|
|
if (!targetMatches.find((mtch) => !mtch)) {
|
|
newArray.splice(i, 1);
|
|
}
|
|
}
|
|
else {
|
|
let existingValue = newArray.find((v) => v[key] == item[key]);
|
|
if (existingValue) {
|
|
newArray.splice(i, 1);
|
|
}
|
|
}
|
|
}
|
|
return newArray;
|
|
}
|