Skip to content

Commit 0fcbcb6

Browse files
Build of package.
1 parent 59a2799 commit 0fcbcb6

File tree

1 file changed

+92
-3
lines changed

1 file changed

+92
-3
lines changed

lib/array-of-objects.js

Lines changed: 92 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
factory();
1111
global.arrayOfObjects = mod.exports;
1212
}
13-
})(this, function () {
13+
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function () {
1414
"use strict";
1515

1616
(function (global, factory) {
@@ -25,24 +25,113 @@
2525
factory(mod.exports);
2626
global.arrayOfObjects = mod.exports;
2727
}
28-
})(void 0, function (_exports) {
28+
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : void 0, function (_exports) {
2929
"use strict";
3030

3131
Object.defineProperty(_exports, "__esModule", {
3232
value: true
3333
});
34+
_exports.findAllMatching = findAllMatching;
35+
_exports.findFirstOneMatching = findFirstOneMatching;
36+
_exports.findLastOneMatching = findLastOneMatching;
3437
_exports.getUniqueValues = getUniqueValues;
38+
_exports.removeAllMatching = removeAllMatching;
39+
_exports.removeFirstOneMatching = removeFirstOneMatching;
40+
_exports.removeLastOneMatching = removeLastOneMatching;
3541

3642
function getUniqueValues(arrayOfObjects, propertyName) {
3743
const output = [];
3844
arrayOfObjects.map(obj => {
3945
if (obj[propertyName] !== undefined) {
40-
if (output.find(val => obj[propertyName] === val) === undefined) {
46+
if (output.find(val => JSON.stringify(obj[propertyName]) === JSON.stringify(val)) === undefined) {
4147
output.push(obj[propertyName]);
4248
}
4349
}
4450
});
4551
return output;
4652
}
53+
54+
function findFirstOneMatching(arrayOfObjects, propertyName, propertyValue) {
55+
let output = null;
56+
arrayOfObjects.some(obj => {
57+
if (obj[propertyName] !== undefined) {
58+
if (JSON.stringify(obj[propertyName]) === JSON.stringify(propertyValue)) {
59+
output = obj;
60+
return true;
61+
}
62+
}
63+
64+
return false;
65+
});
66+
return output;
67+
}
68+
69+
function findLastOneMatching(arrayOfObjects, propertyName, propertyValue) {
70+
let output = null;
71+
arrayOfObjects.map(obj => {
72+
if (obj[propertyName] !== undefined) {
73+
if (JSON.stringify(obj[propertyName]) === JSON.stringify(propertyValue)) {
74+
output = obj;
75+
}
76+
}
77+
});
78+
return output;
79+
}
80+
81+
function findAllMatching(arrayOfObjects, propertyName, propertyValue) {
82+
const output = [];
83+
arrayOfObjects.map(obj => {
84+
if (obj[propertyName] !== undefined) {
85+
if (JSON.stringify(obj[propertyName]) === JSON.stringify(propertyValue)) {
86+
output.push(obj);
87+
}
88+
}
89+
});
90+
return output;
91+
}
92+
93+
function removeFirstOneMatching(arrayOfObjects, propertyName, propertyValue) {
94+
let flag = false;
95+
return arrayOfObjects.filter(obj => {
96+
if (obj[propertyName] !== undefined && !flag) {
97+
if (JSON.stringify(obj[propertyName]) === JSON.stringify(propertyValue)) {
98+
flag = true;
99+
return false;
100+
}
101+
}
102+
103+
return true;
104+
});
105+
}
106+
107+
function removeLastOneMatching(arrayOfObjects, propertyName, propertyValue) {
108+
let lastOneMatchingIndex = -1;
109+
arrayOfObjects.map((obj, index) => {
110+
if (obj[propertyName] !== undefined) {
111+
if (JSON.stringify(obj[propertyName]) === JSON.stringify(propertyValue)) {
112+
lastOneMatchingIndex = index;
113+
}
114+
}
115+
});
116+
const output = JSON.parse(JSON.stringify(arrayOfObjects));
117+
118+
if (lastOneMatchingIndex != -1) {
119+
output.splice(lastOneMatchingIndex, 1);
120+
}
121+
122+
return output;
123+
}
124+
125+
function removeAllMatching(arrayOfObjects, propertyName, propertyValue) {
126+
return arrayOfObjects.filter(obj => {
127+
if (obj[propertyName] !== undefined) {
128+
if (JSON.stringify(obj[propertyName]) === JSON.stringify(propertyValue)) {
129+
return false;
130+
}
131+
}
132+
133+
return true;
134+
});
135+
}
47136
});
48137
});

0 commit comments

Comments
 (0)