Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get unique array based of nested attribute using underscore.js

I have a json array like this :

myArray=[{ a:1,
          b:[{c:"x",d:"y"}, {c:"r", d:"s"}...]
         },
         { a:2,
          b:[c:"p",d:"q"}, {c:"x", d:"s"}...]
         }
         ...
        ]

Is it possible to get a subset of myArray with unique values of "c" using underscore.js?

like image 407
Abdul Avatar asked Jan 22 '26 03:01

Abdul


1 Answers

This should do it:

_.chain(myArray)
 .pluck("b")
 .flatten()
 .pluck("c")
 .unique()
 .value()
like image 180
Bergi Avatar answered Jan 27 '26 02:01

Bergi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!