File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change 1+ const path = require ( 'path' ) ;
2+
3+ const data = require ( path . resolve ( __dirname , 'question.json' ) ) ;
4+
5+ function sortAndFilterUniqueLinks ( arr ) {
6+ const seenLinks = new Set ( ) ;
7+
8+ // Filter objects with unique links
9+ const filteredArr = arr . filter ( obj => {
10+ if ( obj . link && ! seenLinks . has ( obj . link ) ) {
11+ seenLinks . add ( obj . link ) ;
12+ return true ;
13+ }
14+ return false ;
15+ } ) ;
16+
17+ // Sort the filtered array by link alphabetically
18+ return filteredArr . sort ( ( a , b ) => {
19+ if ( a . link < b . link ) return - 1 ;
20+ if ( a . link > b . link ) return 1 ;
21+ return 0 ;
22+ } ) ;
23+ }
24+
25+ const old = data . filter ( ( { link} ) => ! link ) ;
26+ const newData = sortAndFilterUniqueLinks ( data . filter ( ( { link} ) => link ) ) ;
27+ const work = [ ...old , ...newData ] ;
28+
29+ console . log ( JSON . stringify ( work ) ) ;
You can’t perform that action at this time.
0 commit comments