Skip to content

Commit 6ad3cc7

Browse files
Added more functions. Some editions on find function.
1 parent 57e9747 commit 6ad3cc7

File tree

1 file changed

+95
-1
lines changed

1 file changed

+95
-1
lines changed

src/array-of-objects.ts

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,29 @@ export function getUniqueValues(
1717
return output
1818
}
1919

20-
export function findOne(
20+
export function findFirstOneMatching(
21+
arrayOfObjects: Record<string, any>[],
22+
propertyName: any,
23+
propertyValue: any
24+
): Record<string, any> | null {
25+
let output: Record<string, any> | null = null
26+
27+
arrayOfObjects.some(obj => {
28+
if (obj[propertyName] !== undefined) {
29+
if (JSON.stringify(obj[propertyName]) === JSON.stringify(propertyValue)) {
30+
output = obj
31+
32+
return true
33+
}
34+
}
35+
36+
return false
37+
})
38+
39+
return output
40+
}
41+
42+
export function findLastOneMatching(
2143
arrayOfObjects: Record<string, any>[],
2244
propertyName: any,
2345
propertyValue: any
@@ -34,3 +56,75 @@ export function findOne(
3456

3557
return output
3658
}
59+
60+
export function findManyMatching(
61+
arrayOfObjects: Record<string, any>[],
62+
propertyName: any,
63+
propertyValue: any
64+
): Record<string, any>[] {
65+
let output: Record<string, any>[] = []
66+
67+
arrayOfObjects.map(obj => {
68+
if (obj[propertyName] !== undefined) {
69+
if (JSON.stringify(obj[propertyName]) === JSON.stringify(propertyValue)) {
70+
output.push(obj)
71+
}
72+
}
73+
})
74+
75+
return output
76+
}
77+
78+
export function removeFirstOneMatching(
79+
arrayOfObjects: Record<string, any>[],
80+
propertyName: any,
81+
propertyValue: any
82+
): Record<string, any>[] {
83+
let flag: boolean = false
84+
85+
return arrayOfObjects.filter(obj => {
86+
if (obj[propertyName] !== undefined && !flag) {
87+
if (JSON.stringify(obj[propertyName]) === JSON.stringify(propertyValue)) {
88+
flag = true
89+
90+
return false
91+
}
92+
}
93+
94+
return true
95+
})
96+
}
97+
98+
export function removeLastOneMatching(
99+
arrayOfObjects: Record<string, any>[],
100+
propertyName: any,
101+
propertyValue: any
102+
): Record<string, any>[] {
103+
let lastOneMatchingIndex: number = -1
104+
105+
arrayOfObjects.map((obj, index) => {
106+
if (obj[propertyName] !== undefined) {
107+
if (JSON.stringify(obj[propertyName]) === JSON.stringify(propertyValue)) {
108+
lastOneMatchingIndex = index
109+
}
110+
}
111+
})
112+
113+
return lastOneMatchingIndex != -1 ? arrayOfObjects.splice(lastOneMatchingIndex, 1) : arrayOfObjects
114+
}
115+
116+
export function removeAllMatching(
117+
arrayOfObjects: Record<string, any>[],
118+
propertyName: any,
119+
propertyValue: any
120+
): Record<string, any>[] {
121+
return arrayOfObjects.filter(obj => {
122+
if (obj[propertyName] !== undefined) {
123+
if (JSON.stringify(obj[propertyName]) === JSON.stringify(propertyValue)) {
124+
return false
125+
}
126+
}
127+
128+
return true
129+
})
130+
}

0 commit comments

Comments
 (0)