Skip to content

Commit 15b52f1

Browse files
committedFeb 28, 2024
feat: add objFindItemRecursiveByKey method
1 parent c4d9712 commit 15b52f1

File tree

4 files changed

+171
-1
lines changed

4 files changed

+171
-1
lines changed
 

‎CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Change Log
22

3+
## 1.3.3 (2024--)
4+
5+
- `objFindItemRecursiveByKey`: Add `objFindItemRecursiveByKey` method
6+
37
## 1.3.2 (2023-12-28)
48

59
- `strToNumberHash`: Add `strToNumberHash` method

‎docs/src/methods/object.md

+39
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,42 @@ Recursively output all the steps of the JSON object (`JSON.stringify`) and then
4444
```javascript
4545
_.objToPrettyStr({ a: 1, b: { c: 1, d: 2 } }); // Returns '{\n\t"a": 1,\n\t"b": {\n\t\t"c": 1,\n\t\t"d": 2\n\t}\n}'
4646
```
47+
48+
## `_.objFindItemRecursiveByKey`
49+
50+
Returns the object if the key of a specific piece of data in the object's dataset corresponds to a specific value. This function returns only one result, so it is used to search for unique IDs, including all of their children.
51+
52+
### Parameters
53+
54+
- `obj::object`
55+
- `searchKey::string`
56+
- `searchValue::any`
57+
- `childKey::string`
58+
59+
### Returns
60+
61+
> object|null
62+
63+
### Examples
64+
65+
```javascript
66+
_.objFindItemRecursiveByKey(
67+
{
68+
id: 123,
69+
name: 'parent',
70+
child: [
71+
{
72+
id: 456,
73+
name: 'childItemA'
74+
},
75+
{
76+
id: 789,
77+
name: 'childItemB'
78+
}
79+
]
80+
}, // obj
81+
'id', // searchKey
82+
456, // searchValue
83+
'child' // childKey
84+
); // Returns '{ id: 456, name: 'childItemA' }'
85+
```

‎lib/index.ts

+34
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,39 @@ export default class Qsu {
424424
.join('&');
425425
}
426426

427+
static objFindItemRecursiveByKey(
428+
obj: AnyValueObject | any[],
429+
searchKey: string,
430+
searchValue: any,
431+
childKey: string
432+
): AnyValueObject | null {
433+
const findItemFromList = (lists: AnyValueObject): any | null => {
434+
let searchArray: any[];
435+
436+
if (typeof lists !== 'object' || !Array.isArray(lists)) {
437+
searchArray = [lists];
438+
} else {
439+
searchArray = lists;
440+
}
441+
442+
for (let i = 0, iLen = searchArray.length; i < iLen; i += 1) {
443+
if (searchArray[i][searchKey] === searchValue) {
444+
return searchArray[i];
445+
}
446+
if (!Qsu.isEmpty(searchArray[i][childKey])) {
447+
const childItem = findItemFromList(searchArray[i][childKey]);
448+
449+
if (childItem) {
450+
return childItem;
451+
}
452+
}
453+
}
454+
return null;
455+
};
456+
457+
return findItemFromList(obj);
458+
}
459+
427460
static objToPrettyStr(obj: AnyValueObject): string {
428461
return JSON.stringify(obj, null, '\t');
429462
}
@@ -1038,6 +1071,7 @@ export const {
10381071
sortNumeric,
10391072
objToQueryString,
10401073
objToPrettyStr,
1074+
objFindItemRecursiveByKey,
10411075
trim,
10421076
replaceBetween,
10431077
removeSpecialChar,

‎test/object.spec.ts

+94-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from 'assert';
2-
import { objToQueryString, objToPrettyStr } from '../dist';
2+
import { objToQueryString, objToPrettyStr, objFindItemRecursiveByKey } from '../dist';
33

44
describe('Misc', () => {
55
it('funcTimes', (done) => {
@@ -37,4 +37,97 @@ describe('Misc', () => {
3737
);
3838
done();
3939
});
40+
41+
it('objFindItemRecursiveByKey', (done) => {
42+
assert.deepStrictEqual(
43+
objFindItemRecursiveByKey(
44+
{
45+
a: 1,
46+
b: 2,
47+
c: 3
48+
},
49+
'a',
50+
123,
51+
'child'
52+
),
53+
null
54+
);
55+
assert.deepStrictEqual(
56+
objFindItemRecursiveByKey(
57+
[
58+
{
59+
a: 1,
60+
b: 2,
61+
c: 3
62+
},
63+
{
64+
a: 2,
65+
b: 3,
66+
c: 4
67+
},
68+
{
69+
a: 3,
70+
b: 4,
71+
c: 5
72+
},
73+
{
74+
a: 4,
75+
b: 5,
76+
c: 6
77+
}
78+
],
79+
'a',
80+
3,
81+
'a'
82+
),
83+
{
84+
a: 3,
85+
b: 4,
86+
c: 5
87+
}
88+
);
89+
assert.deepStrictEqual(
90+
objFindItemRecursiveByKey(
91+
{
92+
a: {
93+
a: {
94+
a: 123
95+
}
96+
},
97+
b: {
98+
a: {}
99+
},
100+
c: 3
101+
},
102+
'a',
103+
123,
104+
'a'
105+
),
106+
{
107+
a: 123
108+
}
109+
);
110+
assert.deepStrictEqual(
111+
objFindItemRecursiveByKey(
112+
{
113+
id: 123,
114+
child: [
115+
{
116+
id: 456
117+
},
118+
{
119+
id: 789
120+
}
121+
]
122+
},
123+
'id',
124+
456,
125+
'child'
126+
),
127+
{
128+
id: 456
129+
}
130+
);
131+
done();
132+
});
40133
});

0 commit comments

Comments
 (0)
Please sign in to comment.