@@ -10,37 +10,43 @@ export function flattenObject(obj: any, prefix: string = ''): Record<string, any
10
10
const flattened : Record < string , any > = { }
11
11
12
12
for ( const key in obj ) {
13
- if ( Object . prototype . hasOwnProperty . call ( obj , key ) ) {
14
- const value = obj [ key ]
15
- const newKey = prefix ? ` ${ prefix } . ${ key } ` : key
13
+ if ( ! Object . prototype . hasOwnProperty . call ( obj , key ) ) {
14
+ continue
15
+ }
16
16
17
- if ( value && typeof value === 'object' && ! Array . isArray ( value ) ) {
18
- // Handle nested objects
19
- Object . assign ( flattened , flattenObject ( value , newKey ) )
20
- } else if ( Array . isArray ( value ) ) {
21
- // Handle arrays
22
- const isPrimitiveArray = value . every (
23
- ( item : any ) => typeof item !== 'object' || item === null
24
- )
17
+ const value = obj [ key ]
18
+ const newKey = prefix ? `${ prefix } .${ key } ` : key
25
19
26
- if ( isPrimitiveArray ) {
27
- // Handle primitive arrays as single value
28
- flattened [ newKey ] = value
29
- } else {
30
- // Handle arrays containing objects
31
- value . forEach ( ( item : any , index : number ) => {
32
- if ( item && typeof item === 'object' && ! Array . isArray ( item ) ) {
33
- Object . assign ( flattened , flattenObject ( item , `${ newKey } [${ index } ]` ) )
34
- } else {
35
- flattened [ `${ newKey } [${ index } ]` ] = item
36
- }
37
- } )
38
- }
39
- } else {
40
- // Handle primitive values
20
+ if ( value && typeof value === 'object' && ! Array . isArray ( value ) ) {
21
+ // Handle nested objects
22
+ Object . assign ( flattened , flattenObject ( value , newKey ) )
23
+ continue
24
+ }
25
+
26
+ if ( Array . isArray ( value ) ) {
27
+ // Handle arrays
28
+ const isPrimitiveArray = value . every (
29
+ ( item : any ) => typeof item !== 'object' || item === null
30
+ )
31
+
32
+ if ( isPrimitiveArray ) {
33
+ // Handle primitive arrays as single value
41
34
flattened [ newKey ] = value
35
+ } else {
36
+ // Handle arrays containing objects
37
+ value . forEach ( ( item : any , index : number ) => {
38
+ if ( item && typeof item === 'object' && ! Array . isArray ( item ) ) {
39
+ Object . assign ( flattened , flattenObject ( item , `${ newKey } [${ index } ]` ) )
40
+ } else {
41
+ flattened [ `${ newKey } [${ index } ]` ] = item
42
+ }
43
+ } )
42
44
}
45
+ continue
43
46
}
47
+
48
+ // Handle primitive values
49
+ flattened [ newKey ] = value
44
50
}
45
51
46
52
return flattened
0 commit comments