@@ -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,81 @@ export function findOne(
3456
3557 return output
3658}
59+
60+ export function findAllMatching (
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+ let output = JSON . parse ( JSON . stringify ( arrayOfObjects ) )
114+
115+ if ( lastOneMatchingIndex != - 1 ) {
116+ output . splice ( lastOneMatchingIndex , 1 )
117+ }
118+
119+ return output
120+ }
121+
122+ export function removeAllMatching (
123+ arrayOfObjects : Record < string , any > [ ] ,
124+ propertyName : any ,
125+ propertyValue : any
126+ ) : Record < string , any > [ ] {
127+ return arrayOfObjects . filter ( obj => {
128+ if ( obj [ propertyName ] !== undefined ) {
129+ if ( JSON . stringify ( obj [ propertyName ] ) === JSON . stringify ( propertyValue ) ) {
130+ return false
131+ }
132+ }
133+
134+ return true
135+ } )
136+ }
0 commit comments